CMS 3D CMS Logo

Functions | Variables
compile_model Namespace Reference

Functions

def compile_model
 
def main ()
 

Variables

 ld_flag_template
 
 tool_file_template
 
 tool_name_template
 

Function Documentation

◆ compile_model()

def compile_model.compile_model (   subsystem)

Definition at line 32 of file compile_model.py.

References join(), and print().

32  subsystem: str,
33  package: str,
34  model_path: str,
35  model_name: str | None = None,
36  model_version: str = "1.0.0",
37  batch_sizes: tuple[int] = (1,),
38  output_path: str | None = None,
39 ):
40  # default output path
41  if not output_path:
42  # check that we are located in a cmssw env
43  cmssw_base = os.getenv("CMSSW_BASE")
44  if not cmssw_base or not os.path.isdir(cmssw_base):
45  raise Exception("CMSSW_BASE is not set or points to a non-existing directory")
46 
47  output_path = os.path.join("$CMSSW_BASE", "src", subsystem, package, "tfaot_dev")
48  output_path = os.path.expandvars(os.path.expanduser(output_path))
49 
50  # check that the model exists
51  model_path = os.path.expandvars(os.path.expanduser(model_path))
52  model_path = os.path.normpath(os.path.abspath(model_path))
53  if not os.path.exists(model_path):
54  raise Exception(f"model_path '{model_path}' does not exist")
55 
56  # infer the model name when none was provided
57  if not model_name:
58  model_name = os.path.splitext(os.path.basename(model_path))[0]
59 
60  # prepare directories
61  lib_dir = os.path.join(output_path, "lib")
62  if not os.path.exists(lib_dir):
63  os.makedirs(lib_dir)
64  inc_dir = os.path.join(output_path, "include")
65  if not os.path.exists(inc_dir):
66  os.makedirs(inc_dir)
67 
68  # compile the model into a temporary directory
69  from cmsml.scripts.compile_tf_graph import compile_tf_graph
70  with tempfile.TemporaryDirectory() as tmp_dir:
71  compile_tf_graph(
72  model_path=model_path,
73  output_path=tmp_dir,
74  batch_sizes=batch_sizes,
75  compile_prefix=f"{model_name}_bs{{}}",
76  compile_class=f"{subsystem}_{package}::{model_name}_bs{{}}",
77  )
78 
79  # copy files
80  header_files = []
81  for bs in batch_sizes:
82  header_name = f"{model_name}_bs{bs}.h"
83  shutil.copy2(os.path.join(tmp_dir, "aot", header_name), inc_dir)
84  shutil.copy2(os.path.join(tmp_dir, "aot", f"{model_name}_bs{bs}.o"), lib_dir)
85  header_files.append(os.path.join(inc_dir, header_name))
86 
87  # create the wrapper header
88  from create_wrapper import create_wrapper
90  header_files=header_files,
91  model_path=model_path,
92  subsystem=subsystem,
93  package=package,
94  output_path=os.path.join(inc_dir, f"{model_name}.h"),
95  )
96 
97  # create the toolfile
98  tool_vars = {
99  "subsystem": subsystem,
100  "subsystem_uc": subsystem.upper(),
101  "package": package,
102  "package_uc": package.upper(),
103  "model_name": model_name,
104  "model_name_uc": model_name.upper(),
105  "model_version": model_version,
106  "lib_dir_name": os.path.basename(lib_dir),
107  "inc_dir_name": os.path.basename(inc_dir),
108  "tool_name": tool_name_template.format(
109  subsystem=subsystem.lower(),
110  package=package.lower(),
111  model_name=model_name.lower(),
112  ),
113  "ld_flags": "\n ".join([
114  ld_flag_template.format(model_name=model_name, bs=bs)
115  for bs in batch_sizes
116  ]),
117  }
118  tool_path = os.path.join(output_path, f"{tool_vars['tool_name']}.xml")
119  with open(tool_path, "w") as f:
120  f.write(tool_file_template.format(**tool_vars))
121 
122  # print a message
123  tool_path_repr = os.path.relpath(tool_path)
124  if tool_path_repr.startswith(".."):
125  tool_path_repr = tool_path
126  inc_path = f"{output_path}/include/{model_name}.h"
127  if "CMSSW_BASE" in os.environ and os.path.exists(os.environ["CMSSW_BASE"]):
128  inc_path_rel = os.path.relpath(inc_path, os.path.join(os.environ["CMSSW_BASE"], "src"))
129  if not inc_path_rel.startswith(".."):
130  inc_path = inc_path_rel
131 
132  print("\n" + 80 * "-" + "\n")
133  print(f"created custom tool file for AOT compiled model '{model_name}'")
134  print("to register it to scram, run")
135  print(f"\n> scram setup {tool_path_repr}\n")
136  print("and use the following to include it in your code")
137  print(f"\n#include \"{inc_path}\"\n")
138 
139 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:19

◆ main()

def compile_model.main (   None)

Definition at line 140 of file compile_model.py.

References genParticles_cff.map, submitPVValidationJobs.split(), and mkLumiAveragedPlots.tuple.

140 def main() -> None:
141  from argparse import ArgumentParser
142 
143  parser = ArgumentParser(
144  description=__doc__.strip(),
145  )
146  parser.add_argument(
147  "--subsystem",
148  "-s",
149  required=True,
150  help="the CMSSW subsystem that the plugin belongs to",
151  )
152  parser.add_argument(
153  "--package",
154  "-p",
155  required=True,
156  help="the CMSSW package that the plugin belongs to",
157  )
158  parser.add_argument(
159  "--model-path",
160  "-m",
161  required=True,
162  help="the path to the model to compile",
163  )
164  parser.add_argument(
165  "--model-name",
166  default=None,
167  help="a custom model name; when empty, a name is inferred from --model-path",
168  )
169  parser.add_argument(
170  "--model-version",
171  default="1.0.0",
172  help="a custom model version; default: 1.0.0",
173  )
174  parser.add_argument(
175  "--batch-sizes",
176  "-b",
177  default=(1,),
178  type=(lambda s: tuple(map(int, s.strip().split(",")))),
179  help="comma-separated list of batch sizes to compile the model for; default: 1",
180  )
181  parser.add_argument(
182  "--output-path",
183  "-o",
184  help="path where the outputs should be saved; default: "
185  "$CMSSW_BASE/src/SUBSYSTEM/PACKAGE/tfaot_dev",
186  )
187  args = parser.parse_args()
188 
190  subsystem=args.subsystem,
191  package=args.package,
192  model_path=args.model_path,
193  model_name=args.model_name,
194  model_version=args.model_version,
195  batch_sizes=args.batch_sizes,
196  output_path=args.output_path,
197  )
198 
199 

Variable Documentation

◆ ld_flag_template

compile_model.ld_flag_template

Definition at line 16 of file compile_model.py.

◆ tool_file_template

compile_model.tool_file_template

Definition at line 18 of file compile_model.py.

◆ tool_name_template

compile_model.tool_name_template

Definition at line 14 of file compile_model.py.