BestPromptFinder
HomeCoding › Below is an instruction that describes a task. Write a respo...

Below is an instruction that describes a task. Write a respo...

Coding · Gemini, GPT-5.6, Claude · Coding
48Quality
89%Useful
86Reliability

The prompt

Below is an instruction that describes a task. Write a response that appropriately completes the request. ### Instruction: Create a Python function named "Transpile" that takes in four arguments: "src_dir", "dst_dir", "model_filename", and "params_filename". Inside the function, create a CPUPlace object and an Executor object using Paddle Fluid. Then, create a Scope object using fluid.core.Scope(). Load the inference program, feed target names, and fetch targets using fluid.io.load_inference_model() with the "src_dir", "exe", "model_filename", and "params_filename" arguments. Clone the inference program and apply the inference transpiler using fluid.InferenceTranspiler() and t.transpile(). Save the optimized inference program to "dst_dir" using fluid.io.save_persistables() and the "inference_transpiler_program" and "params_filename" arguments. If "dst_dir" does not exist, create it using os.makedirs(). Finally, create a main function that takes in arguments using argparse.ArgumentParser() and calls the "Transpile" function with the parsed arguments. ### Input: ### Output: import os import sys import argparse import paddle.fluid as fluid def Transpile(src_dir, dst_dir, model_filename, params_filename): place = fluid.CPUPlace() exe = fluid.Executor(place) inference_scope = fluid.core.Scope() with fluid.scope_guard(inference_scope): print "Loading inference_program from ", src_dir [inference_program, feed_target_names, fetch_targets] = fluid.io.load_inference_model(src_dir, exe, model_filename, params_filename) inference_transpiler_program = inference_program.clone() # NOTE: Applying the inference transpiler will change the inference_transpiler_program. t = fluid.InferenceTranspiler() # Under the with statement, inference_scope is the global scope. t.transpile(inference_transpiler_program, place) #print inference_transpiler_program print "Saving the optimized inference_program to ", dst_dir # There is a bug in fluid.io.save_inference_model, so we can use the following code instead. if not os.path.isdir(dst_dir): os.makedirs(dst_dir) model_path = os.path.join(dst_dir, model_filename) with open(model_path, "wb") as f: f.write(inference_transpiler_program.desc.serialize_to_string()) fluid.io.save_persistables(exe, dst_dir, inference_transpiler_program, params_filename) def main(): parser = argparse.ArgumentParser() parser.add_argument('--src_dir', help='Source directory of inference model') parser.add_argument('--dst_dir', help='Dstination directory of inference model') parser.add_argument('--model_filename', default=None, help='The name of model file') parser.add_argument('--params_filename', default=None, help='The name of params file') args = parser.parse_args() Transpile(args.src_dir, args.dst_dir, args.model_filename, args.params_filename) if __name__ == '__main__': main()
Find similar in the app →

Why this prompt

Source

Hugging Face

Related Coding prompts

PRD to MVP Technical Plan
Quality 93 · Claude Code
Full-Stack CRUD App Builder
Quality 93 · Claude Code
Legacy Refactor Planner
Quality 93 · Claude Code
Test Suite Generator
Quality 93 · Claude Code
Third-Party API Integration
Quality 93 · Claude Code
Database Schema and Migration
Quality 93 · Claude Code