[aa4946b] | 1 | """ |
---|
| 2 | Core model handling routines. |
---|
| 3 | """ |
---|
[6d6508e] | 4 | from __future__ import print_function |
---|
| 5 | |
---|
[98f60fc] | 6 | __all__ = [ |
---|
[6d6508e] | 7 | "list_models", "load_model", "load_model_info", |
---|
[2547694] | 8 | "build_model", "precompile_dlls", |
---|
[98f60fc] | 9 | ] |
---|
[f734e7d] | 10 | |
---|
[7bf4757] | 11 | import os |
---|
[e65c3ba] | 12 | from os.path import basename, join as joinpath |
---|
[f734e7d] | 13 | from glob import glob |
---|
[e65c3ba] | 14 | import re |
---|
[f734e7d] | 15 | |
---|
[7ae2b7f] | 16 | import numpy as np # type: ignore |
---|
[f734e7d] | 17 | |
---|
[aa4946b] | 18 | from . import generate |
---|
[6d6508e] | 19 | from . import modelinfo |
---|
| 20 | from . import product |
---|
[72a081d] | 21 | from . import mixture |
---|
[aa4946b] | 22 | from . import kernelpy |
---|
[6dba2f0] | 23 | from . import kernelcl |
---|
[aa4946b] | 24 | from . import kerneldll |
---|
[60335cc] | 25 | from . import custom |
---|
[880a2ed] | 26 | |
---|
[2d81cfe] | 27 | # pylint: disable=unused-import |
---|
[f619de7] | 28 | try: |
---|
| 29 | from typing import List, Union, Optional, Any |
---|
| 30 | from .kernel import KernelModel |
---|
[dd7fc12] | 31 | from .modelinfo import ModelInfo |
---|
[f619de7] | 32 | except ImportError: |
---|
| 33 | pass |
---|
[2d81cfe] | 34 | # pylint: enable=unused-import |
---|
[f619de7] | 35 | |
---|
[3221de0] | 36 | CUSTOM_MODEL_PATH = os.environ.get('SAS_MODELPATH', "") |
---|
| 37 | if CUSTOM_MODEL_PATH == "": |
---|
| 38 | CUSTOM_MODEL_PATH = joinpath(os.path.expanduser("~"), ".sasmodels", "custom_models") |
---|
[4341dd4] | 39 | #if not os.path.isdir(CUSTOM_MODEL_PATH): |
---|
| 40 | # os.makedirs(CUSTOM_MODEL_PATH) |
---|
[3221de0] | 41 | |
---|
[4d76711] | 42 | # TODO: refactor composite model support |
---|
| 43 | # The current load_model_info/build_model does not reuse existing model |
---|
| 44 | # definitions when loading a composite model, instead reloading and |
---|
| 45 | # rebuilding the kernel for each component model in the expression. This |
---|
| 46 | # is fine in a scripting environment where the model is built when the script |
---|
| 47 | # starts and is thrown away when the script ends, but may not be the best |
---|
| 48 | # solution in a long-lived application. This affects the following functions: |
---|
| 49 | # |
---|
| 50 | # load_model |
---|
| 51 | # load_model_info |
---|
| 52 | # build_model |
---|
[f734e7d] | 53 | |
---|
[8407d8c] | 54 | KINDS = ("all", "py", "c", "double", "single", "opencl", "1d", "2d", |
---|
[2547694] | 55 | "nonmagnetic", "magnetic") |
---|
[0b03001] | 56 | def list_models(kind=None): |
---|
[52e9a45] | 57 | # type: (str) -> List[str] |
---|
[aa4946b] | 58 | """ |
---|
| 59 | Return the list of available models on the model path. |
---|
[40a87fa] | 60 | |
---|
| 61 | *kind* can be one of the following: |
---|
| 62 | |
---|
| 63 | * all: all models |
---|
| 64 | * py: python models only |
---|
| 65 | * c: compiled models only |
---|
| 66 | * single: models which support single precision |
---|
| 67 | * double: models which require double precision |
---|
[8407d8c] | 68 | * opencl: controls if OpenCL is supperessed |
---|
[40a87fa] | 69 | * 1d: models which are 1D only, or 2D using abs(q) |
---|
| 70 | * 2d: models which can be 2D |
---|
| 71 | * magnetic: models with an sld |
---|
| 72 | * nommagnetic: models without an sld |
---|
[5124c969] | 73 | |
---|
| 74 | For multiple conditions, combine with plus. For example, *c+single+2d* |
---|
| 75 | would return all oriented models implemented in C which can be computed |
---|
| 76 | accurately with single precision arithmetic. |
---|
[aa4946b] | 77 | """ |
---|
[5124c969] | 78 | if kind and any(k not in KINDS for k in kind.split('+')): |
---|
[2547694] | 79 | raise ValueError("kind not in " + ", ".join(KINDS)) |
---|
[3d9001f] | 80 | files = sorted(glob(joinpath(generate.MODEL_PATH, "[a-zA-Z]*.py"))) |
---|
[f734e7d] | 81 | available_models = [basename(f)[:-3] for f in files] |
---|
[5124c969] | 82 | if kind and '+' in kind: |
---|
| 83 | all_kinds = kind.split('+') |
---|
| 84 | condition = lambda name: all(_matches(name, k) for k in all_kinds) |
---|
| 85 | else: |
---|
| 86 | condition = lambda name: _matches(name, kind) |
---|
| 87 | selected = [name for name in available_models if condition(name)] |
---|
[0b03001] | 88 | |
---|
| 89 | return selected |
---|
| 90 | |
---|
| 91 | def _matches(name, kind): |
---|
[2547694] | 92 | if kind is None or kind == "all": |
---|
[0b03001] | 93 | return True |
---|
| 94 | info = load_model_info(name) |
---|
| 95 | pars = info.parameters.kernel_parameters |
---|
| 96 | if kind == "py" and callable(info.Iq): |
---|
| 97 | return True |
---|
| 98 | elif kind == "c" and not callable(info.Iq): |
---|
| 99 | return True |
---|
| 100 | elif kind == "double" and not info.single: |
---|
| 101 | return True |
---|
[d2d6100] | 102 | elif kind == "single" and info.single: |
---|
| 103 | return True |
---|
[8407d8c] | 104 | elif kind == "opencl" and info.opencl: |
---|
[407bf48] | 105 | return True |
---|
[2547694] | 106 | elif kind == "2d" and any(p.type == 'orientation' for p in pars): |
---|
[d2d6100] | 107 | return True |
---|
[40a87fa] | 108 | elif kind == "1d" and all(p.type != 'orientation' for p in pars): |
---|
[0b03001] | 109 | return True |
---|
[2547694] | 110 | elif kind == "magnetic" and any(p.type == 'sld' for p in pars): |
---|
[0b03001] | 111 | return True |
---|
[2547694] | 112 | elif kind == "nonmagnetic" and any(p.type != 'sld' for p in pars): |
---|
[d2d6100] | 113 | return True |
---|
[0b03001] | 114 | return False |
---|
[f734e7d] | 115 | |
---|
[f619de7] | 116 | def load_model(model_name, dtype=None, platform='ocl'): |
---|
[dd7fc12] | 117 | # type: (str, str, str) -> KernelModel |
---|
[b8e5e21] | 118 | """ |
---|
| 119 | Load model info and build model. |
---|
[f619de7] | 120 | |
---|
[2e66ef5] | 121 | *model_name* is the name of the model, or perhaps a model expression |
---|
| 122 | such as sphere*hardsphere or sphere+cylinder. |
---|
| 123 | |
---|
| 124 | *dtype* and *platform* are given by :func:`build_model`. |
---|
[b8e5e21] | 125 | """ |
---|
[f619de7] | 126 | return build_model(load_model_info(model_name), |
---|
| 127 | dtype=dtype, platform=platform) |
---|
[aa4946b] | 128 | |
---|
[61a4bd4] | 129 | def load_model_info(model_string): |
---|
[f619de7] | 130 | # type: (str) -> modelinfo.ModelInfo |
---|
[aa4946b] | 131 | """ |
---|
| 132 | Load a model definition given the model name. |
---|
[1d4017a] | 133 | |
---|
[481ff64] | 134 | *model_string* is the name of the model, or perhaps a model expression |
---|
| 135 | such as sphere*cylinder or sphere+cylinder. Use '@' for a structure |
---|
[60335cc] | 136 | factor product, e.g. sphere@hardsphere. Custom models can be specified by |
---|
| 137 | prefixing the model name with 'custom.', e.g. 'custom.MyModel+sphere'. |
---|
[2e66ef5] | 138 | |
---|
[1d4017a] | 139 | This returns a handle to the module defining the model. This can be |
---|
| 140 | used with functions in generate to build the docs or extract model info. |
---|
[aa4946b] | 141 | """ |
---|
[ffc2a61] | 142 | if "+" in model_string: |
---|
| 143 | parts = [load_model_info(part) |
---|
| 144 | for part in model_string.split("+")] |
---|
| 145 | return mixture.make_mixture_info(parts, operation='+') |
---|
| 146 | elif "*" in model_string: |
---|
| 147 | parts = [load_model_info(part) |
---|
| 148 | for part in model_string.split("*")] |
---|
| 149 | return mixture.make_mixture_info(parts, operation='*') |
---|
[e68bae9] | 150 | elif "@" in model_string: |
---|
| 151 | p_info, q_info = [load_model_info(part) |
---|
| 152 | for part in model_string.split("@")] |
---|
| 153 | return product.make_product_info(p_info, q_info) |
---|
[ffc2a61] | 154 | # We are now dealing with a pure model |
---|
| 155 | elif "custom." in model_string: |
---|
| 156 | pattern = "custom.([A-Za-z0-9_-]+)" |
---|
| 157 | result = re.match(pattern, model_string) |
---|
| 158 | if result is None: |
---|
| 159 | raise ValueError("Model name in invalid format: " + model_string) |
---|
| 160 | model_name = result.group(1) |
---|
| 161 | # Use ModelName to find the path to the custom model file |
---|
| 162 | model_path = joinpath(CUSTOM_MODEL_PATH, model_name + ".py") |
---|
| 163 | if not os.path.isfile(model_path): |
---|
| 164 | raise ValueError("The model file {} doesn't exist".format(model_path)) |
---|
| 165 | kernel_module = custom.load_custom_kernel_module(model_path) |
---|
| 166 | return modelinfo.make_model_info(kernel_module) |
---|
| 167 | kernel_module = generate.load_kernel_module(model_string) |
---|
| 168 | return modelinfo.make_model_info(kernel_module) |
---|
[d19962c] | 169 | |
---|
| 170 | |
---|
[17bbadd] | 171 | def build_model(model_info, dtype=None, platform="ocl"): |
---|
[dd7fc12] | 172 | # type: (modelinfo.ModelInfo, str, str) -> KernelModel |
---|
[aa4946b] | 173 | """ |
---|
| 174 | Prepare the model for the default execution platform. |
---|
| 175 | |
---|
| 176 | This will return an OpenCL model, a DLL model or a python model depending |
---|
| 177 | on the model and the computing platform. |
---|
| 178 | |
---|
[17bbadd] | 179 | *model_info* is the model definition structure returned from |
---|
| 180 | :func:`load_model_info`. |
---|
[bcd3aa3] | 181 | |
---|
[aa4946b] | 182 | *dtype* indicates whether the model should use single or double precision |
---|
[dd7fc12] | 183 | for the calculation. Choices are 'single', 'double', 'quad', 'half', |
---|
| 184 | or 'fast'. If *dtype* ends with '!', then force the use of the DLL rather |
---|
| 185 | than OpenCL for the calculation. |
---|
[aa4946b] | 186 | |
---|
| 187 | *platform* should be "dll" to force the dll to be used for C models, |
---|
| 188 | otherwise it uses the default "ocl". |
---|
| 189 | """ |
---|
[6d6508e] | 190 | composition = model_info.composition |
---|
[72a081d] | 191 | if composition is not None: |
---|
| 192 | composition_type, parts = composition |
---|
| 193 | models = [build_model(p, dtype=dtype, platform=platform) for p in parts] |
---|
| 194 | if composition_type == 'mixture': |
---|
| 195 | return mixture.MixtureModel(model_info, models) |
---|
| 196 | elif composition_type == 'product': |
---|
[e79f0a5] | 197 | P, S = models |
---|
[72a081d] | 198 | return product.ProductModel(model_info, P, S) |
---|
| 199 | else: |
---|
| 200 | raise ValueError('unknown mixture type %s'%composition_type) |
---|
[aa4946b] | 201 | |
---|
[fa5fd8d] | 202 | # If it is a python model, return it immediately |
---|
| 203 | if callable(model_info.Iq): |
---|
| 204 | return kernelpy.PyModel(model_info) |
---|
| 205 | |
---|
[def2c1b] | 206 | numpy_dtype, fast, platform = parse_dtype(model_info, dtype, platform) |
---|
[7891a2a] | 207 | |
---|
[72a081d] | 208 | source = generate.make_source(model_info) |
---|
[7891a2a] | 209 | if platform == "dll": |
---|
[f2f67a6] | 210 | #print("building dll", numpy_dtype) |
---|
[a4280bd] | 211 | return kerneldll.load_dll(source['dll'], model_info, numpy_dtype) |
---|
[aa4946b] | 212 | else: |
---|
[f2f67a6] | 213 | #print("building ocl", numpy_dtype) |
---|
[dd7fc12] | 214 | return kernelcl.GpuModel(source, model_info, numpy_dtype, fast=fast) |
---|
[aa4946b] | 215 | |
---|
[7bf4757] | 216 | def precompile_dlls(path, dtype="double"): |
---|
[7891a2a] | 217 | # type: (str, str) -> List[str] |
---|
[b8e5e21] | 218 | """ |
---|
[7bf4757] | 219 | Precompile the dlls for all builtin models, returning a list of dll paths. |
---|
[b8e5e21] | 220 | |
---|
[7bf4757] | 221 | *path* is the directory in which to save the dlls. It will be created if |
---|
| 222 | it does not already exist. |
---|
[b8e5e21] | 223 | |
---|
| 224 | This can be used when build the windows distribution of sasmodels |
---|
[7bf4757] | 225 | which may be missing the OpenCL driver and the dll compiler. |
---|
[b8e5e21] | 226 | """ |
---|
[7891a2a] | 227 | numpy_dtype = np.dtype(dtype) |
---|
[7bf4757] | 228 | if not os.path.exists(path): |
---|
| 229 | os.makedirs(path) |
---|
| 230 | compiled_dlls = [] |
---|
| 231 | for model_name in list_models(): |
---|
| 232 | model_info = load_model_info(model_name) |
---|
[a4280bd] | 233 | if not callable(model_info.Iq): |
---|
| 234 | source = generate.make_source(model_info)['dll'] |
---|
[2dcd6e7] | 235 | old_path = kerneldll.SAS_DLL_PATH |
---|
[7bf4757] | 236 | try: |
---|
[2dcd6e7] | 237 | kerneldll.SAS_DLL_PATH = path |
---|
[def2c1b] | 238 | dll = kerneldll.make_dll(source, model_info, dtype=numpy_dtype) |
---|
[7bf4757] | 239 | finally: |
---|
[2dcd6e7] | 240 | kerneldll.SAS_DLL_PATH = old_path |
---|
[7bf4757] | 241 | compiled_dlls.append(dll) |
---|
| 242 | return compiled_dlls |
---|
[b8e5e21] | 243 | |
---|
[7891a2a] | 244 | def parse_dtype(model_info, dtype=None, platform=None): |
---|
| 245 | # type: (ModelInfo, str, str) -> (np.dtype, bool, str) |
---|
[dd7fc12] | 246 | """ |
---|
| 247 | Interpret dtype string, returning np.dtype and fast flag. |
---|
| 248 | |
---|
| 249 | Possible types include 'half', 'single', 'double' and 'quad'. If the |
---|
[2e66ef5] | 250 | type is 'fast', then this is equivalent to dtype 'single' but using |
---|
[9e771a3] | 251 | fast native functions rather than those with the precision level |
---|
| 252 | guaranteed by the OpenCL standard. 'default' will choose the appropriate |
---|
| 253 | default for the model and platform. |
---|
[2e66ef5] | 254 | |
---|
| 255 | Platform preference can be specfied ("ocl" vs "dll"), with the default |
---|
| 256 | being OpenCL if it is availabe. If the dtype name ends with '!' then |
---|
| 257 | platform is forced to be DLL rather than OpenCL. |
---|
| 258 | |
---|
| 259 | This routine ignores the preferences within the model definition. This |
---|
| 260 | is by design. It allows us to test models in single precision even when |
---|
| 261 | we have flagged them as requiring double precision so we can easily check |
---|
| 262 | the performance on different platforms without having to change the model |
---|
| 263 | definition. |
---|
[dd7fc12] | 264 | """ |
---|
[7891a2a] | 265 | # Assign default platform, overriding ocl with dll if OpenCL is unavailable |
---|
[8407d8c] | 266 | # If opencl=False OpenCL is switched off |
---|
[fe77343] | 267 | |
---|
[f49675c] | 268 | if platform is None: |
---|
[7891a2a] | 269 | platform = "ocl" |
---|
[3221de0] | 270 | if not kernelcl.use_opencl() or not model_info.opencl: |
---|
[7891a2a] | 271 | platform = "dll" |
---|
| 272 | |
---|
| 273 | # Check if type indicates dll regardless of which platform is given |
---|
| 274 | if dtype is not None and dtype.endswith('!'): |
---|
| 275 | platform = "dll" |
---|
[dd7fc12] | 276 | dtype = dtype[:-1] |
---|
| 277 | |
---|
[7891a2a] | 278 | # Convert special type names "half", "fast", and "quad" |
---|
[2547694] | 279 | fast = (dtype == "fast") |
---|
[7891a2a] | 280 | if fast: |
---|
| 281 | dtype = "single" |
---|
[2547694] | 282 | elif dtype == "quad": |
---|
[7891a2a] | 283 | dtype = "longdouble" |
---|
[2547694] | 284 | elif dtype == "half": |
---|
[650c6d2] | 285 | dtype = "float16" |
---|
[7891a2a] | 286 | |
---|
| 287 | # Convert dtype string to numpy dtype. |
---|
[bb39b4a] | 288 | if dtype is None or dtype == "default": |
---|
[2547694] | 289 | numpy_dtype = (generate.F32 if platform == "ocl" and model_info.single |
---|
| 290 | else generate.F64) |
---|
[dd7fc12] | 291 | else: |
---|
[7891a2a] | 292 | numpy_dtype = np.dtype(dtype) |
---|
[9890053] | 293 | |
---|
[7891a2a] | 294 | # Make sure that the type is supported by opencl, otherwise use dll |
---|
[2547694] | 295 | if platform == "ocl": |
---|
[7891a2a] | 296 | env = kernelcl.environment() |
---|
| 297 | if not env.has_type(numpy_dtype): |
---|
| 298 | platform = "dll" |
---|
| 299 | if dtype is None: |
---|
| 300 | numpy_dtype = generate.F64 |
---|
[dd7fc12] | 301 | |
---|
[7891a2a] | 302 | return numpy_dtype, fast, platform |
---|
[0c24a82] | 303 | |
---|
[2547694] | 304 | def list_models_main(): |
---|
[40a87fa] | 305 | # type: () -> None |
---|
| 306 | """ |
---|
| 307 | Run list_models as a main program. See :func:`list_models` for the |
---|
| 308 | kinds of models that can be requested on the command line. |
---|
| 309 | """ |
---|
[0b03001] | 310 | import sys |
---|
| 311 | kind = sys.argv[1] if len(sys.argv) > 1 else "all" |
---|
| 312 | print("\n".join(list_models(kind))) |
---|
[2547694] | 313 | |
---|
[a69d8cd] | 314 | def test_composite_order(): |
---|
[7a516d0] | 315 | def test_models(fst, snd): |
---|
| 316 | """Confirm that two models produce the same parameters""" |
---|
| 317 | fst = load_model(fst) |
---|
| 318 | snd = load_model(snd) |
---|
[a69d8cd] | 319 | # Un-disambiguate parameter names so that we can check if the same |
---|
| 320 | # parameters are in a pair of composite models. Since each parameter in |
---|
| 321 | # the mixture model is tagged as e.g., A_sld, we ought to use a |
---|
| 322 | # regex subsitution s/^[A-Z]+_/_/, but removing all uppercase letters |
---|
| 323 | # is good enough. |
---|
[7a516d0] | 324 | fst = [[x for x in p.name if x == x.lower()] for p in fst.info.parameters.kernel_parameters] |
---|
| 325 | snd = [[x for x in p.name if x == x.lower()] for p in snd.info.parameters.kernel_parameters] |
---|
| 326 | assert sorted(fst) == sorted(snd), "{} != {}".format(fst, snd) |
---|
| 327 | |
---|
[a69d8cd] | 328 | def build_test(first, second): |
---|
| 329 | test = lambda description: test_models(first, second) |
---|
| 330 | description = first + " vs. " + second |
---|
| 331 | return test, description |
---|
[7a516d0] | 332 | |
---|
[a69d8cd] | 333 | yield build_test( |
---|
[7a516d0] | 334 | "cylinder+sphere", |
---|
| 335 | "sphere+cylinder") |
---|
[a69d8cd] | 336 | yield build_test( |
---|
[7a516d0] | 337 | "cylinder*sphere", |
---|
| 338 | "sphere*cylinder") |
---|
[a69d8cd] | 339 | yield build_test( |
---|
[7a516d0] | 340 | "cylinder@hardsphere*sphere", |
---|
| 341 | "sphere*cylinder@hardsphere") |
---|
[a69d8cd] | 342 | yield build_test( |
---|
[7a516d0] | 343 | "barbell+sphere*cylinder@hardsphere", |
---|
| 344 | "sphere*cylinder@hardsphere+barbell") |
---|
[a69d8cd] | 345 | yield build_test( |
---|
[7a516d0] | 346 | "barbell+cylinder@hardsphere*sphere", |
---|
| 347 | "cylinder@hardsphere*sphere+barbell") |
---|
[a69d8cd] | 348 | yield build_test( |
---|
[7a516d0] | 349 | "barbell+sphere*cylinder@hardsphere", |
---|
| 350 | "barbell+cylinder@hardsphere*sphere") |
---|
[a69d8cd] | 351 | yield build_test( |
---|
[7a516d0] | 352 | "sphere*cylinder@hardsphere+barbell", |
---|
| 353 | "cylinder@hardsphere*sphere+barbell") |
---|
[a69d8cd] | 354 | yield build_test( |
---|
[7a516d0] | 355 | "barbell+sphere*cylinder@hardsphere", |
---|
| 356 | "cylinder@hardsphere*sphere+barbell") |
---|
[a69d8cd] | 357 | yield build_test( |
---|
[7a516d0] | 358 | "barbell+cylinder@hardsphere*sphere", |
---|
| 359 | "sphere*cylinder@hardsphere+barbell") |
---|
| 360 | |
---|
[a69d8cd] | 361 | def test_composite(): |
---|
| 362 | # type: () -> None |
---|
| 363 | """Check that model load works""" |
---|
[7a516d0] | 364 | #Test the the model produces the parameters that we would expect |
---|
| 365 | model = load_model("cylinder@hardsphere*sphere") |
---|
| 366 | actual = [p.name for p in model.info.parameters.kernel_parameters] |
---|
| 367 | target = ("sld sld_solvent radius length theta phi volfraction" |
---|
| 368 | " A_sld A_sld_solvent A_radius").split() |
---|
| 369 | assert target == actual, "%s != %s"%(target, actual) |
---|
| 370 | |
---|
[2547694] | 371 | if __name__ == "__main__": |
---|
| 372 | list_models_main() |
---|