def generate_code(args): LOG.propagate = False nntool_shell = NNToolShell(args, allow_cli_args=False) # handler = GeneratorShellLogHandler() # formatter = logging.Formatter('%(module)s - %(message)s') # handler.setFormatter(formatter) # LOG.addHandler(handler) # if args.log_level: # LOG.setLevel(args.log_level.upper()) # else: # LOG.setLevel('WARN') LOG.info("Loading %s", args.graph_file) nntool_shell.load_state_file(args.graph_file) G = nntool_shell.G opts = nntool_shell.settings if args.model_file: opts['model_file'] = args.model_file if args.model_directory: opts['model_directory'] = args.model_directory if args.tensor_directory: opts['tensor_directory'] = args.tensor_directory opts['basic_kernel_header_file'] = args.model_file opts['basic_kernel_source_file'] = args.model_file os.makedirs(os.path.abspath(opts['model_directory']), mode=0o750, exist_ok=True) os.makedirs(os.path.abspath(opts['tensor_directory']), mode=0o750, exist_ok=True) code_gen = CodeGenerator(G, DefaultNamingConvension(G), opts) if args.template_file: code_template = dynamic_template(args.template_file) else: code_template = default_template write_template(G, code_gen, opts['model_directory'], opts['model_file'], code_template, "model") if G.has_expressions: write_template(G, code_gen, opts['model_directory'], opts['basic_kernel_header_file'], basic_kernel_header_template, "kernel headers") write_template(G, code_gen, opts['model_directory'], opts['basic_kernel_source_file'], basic_kernel_source_template, "kernel source") if args.header_file: with open(os.path.join(opts['model_directory'], args.header_file), "w") as output_fp: output_fp.write(header_template(G, code_generator=code_gen)) if not args.dont_dump_tensors: LOG.info("Writing constants to %s", opts['model_directory']) code_gen.write_constants()
def do_gen(self, args): """ Generate AutoTiler model C code and optionally dump tensors. If no destination file is given the generated code will be outputed to the screen. Check the 'set' command for settings related to code generation.""" self._check_graph() self._check_quantized() self._check_adjusted() if args.checksums: input_args = self._get_input_args(None) LOG.info("input file %s", args.checksums) data = import_data(args.checksums, **input_args) executer = GraphExecuter(self.G, qrecs=self.G.quantization) executer.execute([data], qmode=QuantizationMode.all()) self.settings['checksum_file'] = args.checksums self.settings['generate_checksums'] = True if args.tensor_directory: self.settings['tensor_directory'] = args.tensor_directory if args.model_directory: self.settings['model_directory'] = args.model_directory self.settings['basic_kernel_source_file'] = args.basic_kernel_source_file self.settings['basic_kernel_header_file'] = args.basic_kernel_header_file code_gen = CodeGenerator(self.G, DefaultNamingConvension(self.G), self.settings) if self.settings['template_file']: code_template = dynamic_template(self.settings['template_file']) else: code_template = default_template if args.model_file: with open(os.path.join(self.settings['model_directory'], args.model_file), "w") as output_fp: output_fp.write(code_template(self.G, code_generator=code_gen)) if self.G.has_expressions: with open(os.path.join(self.settings['model_directory'], args.basic_kernel_source_file), "w") as output_fp: output_fp.write(basic_kernel_source_template(self.G, code_generator=code_gen)) with open(os.path.join(self.settings['model_directory'], args.basic_kernel_header_file), "w") as output_fp: output_fp.write(basic_kernel_header_template(self.G, code_generator=code_gen)) else: self.ppaged(code_template(self.G, code_generator=code_gen)) if self.G.has_expressions: self.ppaged(basic_kernel_source_template(self.G, code_generator=code_gen)) self.ppaged(basic_kernel_header_template(self.G, code_generator=code_gen)) if args.output_tensors: code_gen.write_constants() if args.header_file: with open(os.path.join(self.settings['model_directory'], args.header_file), "w") as output_fp: output_fp.write(header_template(self.G, code_generator=code_gen))
def generate_code(args): LOG.propagate = False handler = GeneratorShellLogHandler() formatter = logging.Formatter('%(module)s - %(message)s') handler.setFormatter(formatter) LOG.addHandler(handler) if args.log_level: LOG.setLevel(args.log_level.upper()) else: LOG.setLevel('WARN') LOG.info("Loading %s", args.graph_file) G, opts = load_state(args.graph_file, return_extra=True) if args.model_file: opts['model_file'] = args.model_file if args.model_directory: opts['model_directory'] = args.model_directory if args.tensor_directory: opts['tensor_directory'] = args.tensor_directory os.makedirs(os.path.abspath(opts['model_directory']), mode=0o750, exist_ok=True) os.makedirs(os.path.abspath(opts['tensor_directory']), mode=0o750, exist_ok=True) model_path = os.path.join(opts['model_directory'], opts['model_file']) LOG.info("Saving model to %s", model_path) code_gen = CodeGenerator(G, DefaultNamingConvension(G), opts) if args.template_file: code_template = dynamic_template(args.template_file) else: code_template = default_template model = code_template(G, code_generator=code_gen) if not model: LOG.error("error generating model code") sys.exit(1) with open(model_path, "w") as output_fp: output_fp.write(model) if args.header_file: with open(os.path.join(opts['model_directory'], args.header_file), "w") as output_fp: output_fp.write(header_template(G, code_generator=code_gen)) if not args.dont_dump_tensors: LOG.info("Writing constants to %s", opts['model_directory']) code_gen.write_constants()