madspace 0.3.1__cp311-cp311-macosx_14_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- madspace/__init__.py +1 -0
- madspace/_madspace_py.cpython-311-darwin.so +0 -0
- madspace/_madspace_py.pyi +2189 -0
- madspace/_madspace_py_loader.py +111 -0
- madspace/include/madspace/constants.h +17 -0
- madspace/include/madspace/madcode/function.h +102 -0
- madspace/include/madspace/madcode/function_builder_mixin.h +591 -0
- madspace/include/madspace/madcode/instruction.h +208 -0
- madspace/include/madspace/madcode/opcode_mixin.h +134 -0
- madspace/include/madspace/madcode/optimizer.h +31 -0
- madspace/include/madspace/madcode/type.h +203 -0
- madspace/include/madspace/madcode.h +6 -0
- madspace/include/madspace/phasespace/base.h +74 -0
- madspace/include/madspace/phasespace/channel_weight_network.h +46 -0
- madspace/include/madspace/phasespace/channel_weights.h +51 -0
- madspace/include/madspace/phasespace/chili.h +32 -0
- madspace/include/madspace/phasespace/cross_section.h +47 -0
- madspace/include/madspace/phasespace/cuts.h +34 -0
- madspace/include/madspace/phasespace/discrete_flow.h +44 -0
- madspace/include/madspace/phasespace/discrete_sampler.h +53 -0
- madspace/include/madspace/phasespace/flow.h +53 -0
- madspace/include/madspace/phasespace/histograms.h +26 -0
- madspace/include/madspace/phasespace/integrand.h +204 -0
- madspace/include/madspace/phasespace/invariants.h +26 -0
- madspace/include/madspace/phasespace/luminosity.h +41 -0
- madspace/include/madspace/phasespace/matrix_element.h +70 -0
- madspace/include/madspace/phasespace/mlp.h +37 -0
- madspace/include/madspace/phasespace/multichannel.h +49 -0
- madspace/include/madspace/phasespace/observable.h +85 -0
- madspace/include/madspace/phasespace/pdf.h +78 -0
- madspace/include/madspace/phasespace/phasespace.h +67 -0
- madspace/include/madspace/phasespace/rambo.h +26 -0
- madspace/include/madspace/phasespace/scale.h +52 -0
- madspace/include/madspace/phasespace/t_propagator_mapping.h +34 -0
- madspace/include/madspace/phasespace/three_particle.h +68 -0
- madspace/include/madspace/phasespace/topology.h +116 -0
- madspace/include/madspace/phasespace/two_particle.h +63 -0
- madspace/include/madspace/phasespace/vegas.h +53 -0
- madspace/include/madspace/phasespace.h +27 -0
- madspace/include/madspace/runtime/context.h +147 -0
- madspace/include/madspace/runtime/discrete_optimizer.h +24 -0
- madspace/include/madspace/runtime/event_generator.h +257 -0
- madspace/include/madspace/runtime/format.h +68 -0
- madspace/include/madspace/runtime/io.h +343 -0
- madspace/include/madspace/runtime/lhe_output.h +132 -0
- madspace/include/madspace/runtime/logger.h +46 -0
- madspace/include/madspace/runtime/runtime_base.h +39 -0
- madspace/include/madspace/runtime/tensor.h +603 -0
- madspace/include/madspace/runtime/thread_pool.h +101 -0
- madspace/include/madspace/runtime/vegas_optimizer.h +26 -0
- madspace/include/madspace/runtime.h +12 -0
- madspace/include/madspace/umami.h +202 -0
- madspace/include/madspace/util.h +142 -0
- madspace/lib/libmadspace.dylib +0 -0
- madspace/lib/libmadspace_cpu.dylib +0 -0
- madspace/madnis/__init__.py +44 -0
- madspace/madnis/buffer.py +167 -0
- madspace/madnis/channel_grouping.py +85 -0
- madspace/madnis/distribution.py +103 -0
- madspace/madnis/integrand.py +175 -0
- madspace/madnis/integrator.py +973 -0
- madspace/madnis/interface.py +191 -0
- madspace/madnis/losses.py +186 -0
- madspace/torch.py +82 -0
- madspace-0.3.1.dist-info/METADATA +71 -0
- madspace-0.3.1.dist-info/RECORD +68 -0
- madspace-0.3.1.dist-info/WHEEL +6 -0
- madspace-0.3.1.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import ctypes
|
|
2
|
+
import logging
|
|
3
|
+
import os
|
|
4
|
+
import platform
|
|
5
|
+
|
|
6
|
+
# pre-load libmadspace
|
|
7
|
+
ctypes.CDLL(
|
|
8
|
+
os.path.join(
|
|
9
|
+
os.path.dirname(os.path.abspath(__file__)),
|
|
10
|
+
"lib",
|
|
11
|
+
"libmadspace.dylib" if platform.system() == "Darwin" else "libmadspace.so",
|
|
12
|
+
),
|
|
13
|
+
mode=ctypes.RTLD_GLOBAL,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
from ._madspace_py import *
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _init():
|
|
20
|
+
"""
|
|
21
|
+
Monkey-patch classes for a more pythonic experience.
|
|
22
|
+
"""
|
|
23
|
+
set_lib_path(os.path.join(os.path.dirname(os.path.abspath(__file__)), "lib"))
|
|
24
|
+
|
|
25
|
+
def call_and_convert(runtime, args):
|
|
26
|
+
if len(args) == 0:
|
|
27
|
+
tensorlib = "numpy"
|
|
28
|
+
else:
|
|
29
|
+
tensorlib = type(args[0]).__module__
|
|
30
|
+
outputs = runtime.call(args)
|
|
31
|
+
# Convert outputs, lazy-loading torch or numpy
|
|
32
|
+
if tensorlib == "torch":
|
|
33
|
+
import torch
|
|
34
|
+
|
|
35
|
+
return tuple(torch.from_dlpack(out) for out in outputs)
|
|
36
|
+
else:
|
|
37
|
+
import numpy
|
|
38
|
+
|
|
39
|
+
return tuple(numpy.from_dlpack(out) for out in outputs)
|
|
40
|
+
|
|
41
|
+
def runtime_call(self, *args):
|
|
42
|
+
outputs = call_and_convert(self, args)
|
|
43
|
+
if len(outputs) == 1:
|
|
44
|
+
return outputs[0]
|
|
45
|
+
else:
|
|
46
|
+
return outputs
|
|
47
|
+
|
|
48
|
+
def function_call(self, *args):
|
|
49
|
+
if not hasattr(self, "runtime"):
|
|
50
|
+
self.runtime = FunctionRuntime(self)
|
|
51
|
+
outputs = call_and_convert(self.runtime, args)
|
|
52
|
+
if len(outputs) == 1:
|
|
53
|
+
return outputs[0]
|
|
54
|
+
else:
|
|
55
|
+
return outputs
|
|
56
|
+
|
|
57
|
+
def function_generator_call(self, *args):
|
|
58
|
+
if not hasattr(self, "runtime"):
|
|
59
|
+
self.runtime = FunctionRuntime(self.function())
|
|
60
|
+
outputs = call_and_convert(self.runtime, args)
|
|
61
|
+
if len(outputs) == 1:
|
|
62
|
+
return outputs[0]
|
|
63
|
+
else:
|
|
64
|
+
return outputs
|
|
65
|
+
|
|
66
|
+
def map_forward(self, inputs, conditions=[]):
|
|
67
|
+
if not hasattr(self, "forward_runtime"):
|
|
68
|
+
self.forward_runtime = FunctionRuntime(self.forward_function())
|
|
69
|
+
outputs = call_and_convert(self.forward_runtime, [*inputs, *conditions])
|
|
70
|
+
return outputs[:-1], outputs[-1]
|
|
71
|
+
|
|
72
|
+
def map_inverse(self, inputs, conditions=[]):
|
|
73
|
+
if not hasattr(self, "inverse_runtime"):
|
|
74
|
+
self.inverse_runtime = FunctionRuntime(self.inverse_function())
|
|
75
|
+
outputs = call_and_convert(self.inverse_runtime, [*inputs, *conditions])
|
|
76
|
+
return outputs[:-1], outputs[-1]
|
|
77
|
+
|
|
78
|
+
def tensor_numpy(tensor):
|
|
79
|
+
import numpy # Lazy-load numpy, to make it optional dependency
|
|
80
|
+
|
|
81
|
+
return numpy.from_dlpack(tensor)
|
|
82
|
+
|
|
83
|
+
def tensor_torch(tensor):
|
|
84
|
+
import torch # Lazy-load torch, to make it optional dependency
|
|
85
|
+
|
|
86
|
+
return torch.from_dlpack(tensor)
|
|
87
|
+
|
|
88
|
+
py_logger = logging.getLogger("madspace")
|
|
89
|
+
|
|
90
|
+
def log_handler(level, message):
|
|
91
|
+
match level:
|
|
92
|
+
case Logger.level_debug:
|
|
93
|
+
py_logger.debug(message)
|
|
94
|
+
case Logger.level_info:
|
|
95
|
+
py_logger.info(message)
|
|
96
|
+
case Logger.level_warning:
|
|
97
|
+
py_logger.warning(message)
|
|
98
|
+
case Logger.level_error:
|
|
99
|
+
py_logger.error(message)
|
|
100
|
+
|
|
101
|
+
FunctionRuntime.__call__ = runtime_call
|
|
102
|
+
Function.__call__ = function_call
|
|
103
|
+
FunctionGenerator.__call__ = function_generator_call
|
|
104
|
+
Mapping.map_forward = map_forward
|
|
105
|
+
Mapping.map_inverse = map_inverse
|
|
106
|
+
Tensor.numpy = tensor_numpy
|
|
107
|
+
Tensor.torch = tensor_torch
|
|
108
|
+
# Logger.set_log_handler(log_handler)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
_init()
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
namespace madspace {
|
|
4
|
+
|
|
5
|
+
// mathematical constants
|
|
6
|
+
|
|
7
|
+
inline constexpr double PI = 3.14159265358979323846;
|
|
8
|
+
inline constexpr double LOG_TWO = 0.69314718055994530942;
|
|
9
|
+
inline constexpr double SQRT_HALF = 0.70710678118654752440;
|
|
10
|
+
inline constexpr double TWO_DIV_SQRT_PI = 1.12837916709551257390;
|
|
11
|
+
|
|
12
|
+
// spline flow settings
|
|
13
|
+
|
|
14
|
+
inline constexpr double MIN_BIN_SIZE = 1e-3;
|
|
15
|
+
inline constexpr double MIN_DERIVATIVE = 1e-3;
|
|
16
|
+
|
|
17
|
+
} // namespace madspace
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <iostream>
|
|
4
|
+
#include <map>
|
|
5
|
+
#include <optional>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <vector>
|
|
8
|
+
|
|
9
|
+
#include <nlohmann/json.hpp>
|
|
10
|
+
|
|
11
|
+
#include "instruction.h"
|
|
12
|
+
|
|
13
|
+
namespace madspace {
|
|
14
|
+
|
|
15
|
+
struct InstructionCall {
|
|
16
|
+
InstructionPtr instruction;
|
|
17
|
+
ValueVec inputs;
|
|
18
|
+
ValueVec outputs;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
class Function {
|
|
22
|
+
public:
|
|
23
|
+
friend class FunctionBuilder;
|
|
24
|
+
|
|
25
|
+
Function() = default;
|
|
26
|
+
|
|
27
|
+
const ValueVec& inputs() const { return _inputs; }
|
|
28
|
+
const ValueVec& outputs() const { return _outputs; }
|
|
29
|
+
const ValueVec& locals() const { return _locals; }
|
|
30
|
+
const std::unordered_map<std::string, Value>& globals() const { return _globals; }
|
|
31
|
+
const std::vector<InstructionCall>& instructions() const { return _instructions; }
|
|
32
|
+
|
|
33
|
+
void save(const std::string& file) const;
|
|
34
|
+
static Function load(const std::string& file);
|
|
35
|
+
|
|
36
|
+
private:
|
|
37
|
+
Function(
|
|
38
|
+
const ValueVec& inputs,
|
|
39
|
+
const ValueVec& outputs,
|
|
40
|
+
const ValueVec& locals,
|
|
41
|
+
const std::unordered_map<std::string, Value>& globals,
|
|
42
|
+
const std::vector<InstructionCall>& instructions
|
|
43
|
+
) :
|
|
44
|
+
_inputs(inputs),
|
|
45
|
+
_outputs(outputs),
|
|
46
|
+
_locals(locals),
|
|
47
|
+
_globals(globals),
|
|
48
|
+
_instructions(instructions) {}
|
|
49
|
+
|
|
50
|
+
ValueVec _inputs;
|
|
51
|
+
ValueVec _outputs;
|
|
52
|
+
ValueVec _locals;
|
|
53
|
+
std::unordered_map<std::string, Value> _globals;
|
|
54
|
+
std::vector<InstructionCall> _instructions;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
std::ostream& operator<<(std::ostream& out, const Value& value);
|
|
58
|
+
std::ostream& operator<<(std::ostream& out, const ValueVec& list);
|
|
59
|
+
std::ostream& operator<<(std::ostream& out, const InstructionCall& call);
|
|
60
|
+
std::ostream& operator<<(std::ostream& out, const Function& func);
|
|
61
|
+
|
|
62
|
+
void to_json(nlohmann::json& j, const InstructionCall& call);
|
|
63
|
+
void to_json(nlohmann::json& j, const Function& call);
|
|
64
|
+
void from_json(const nlohmann::json& j, Function& call);
|
|
65
|
+
|
|
66
|
+
class FunctionBuilder {
|
|
67
|
+
public:
|
|
68
|
+
FunctionBuilder(
|
|
69
|
+
const std::vector<Type> _input_types, const std::vector<Type> _output_types
|
|
70
|
+
);
|
|
71
|
+
FunctionBuilder(const Function& function);
|
|
72
|
+
Value input(int index) const;
|
|
73
|
+
ValueVec input_range(int start_index, int end_index) const;
|
|
74
|
+
void output(int index, Value value);
|
|
75
|
+
void output_range(int start_index, const ValueVec& values);
|
|
76
|
+
Value
|
|
77
|
+
global(const std::string& name, DataType dtype, const std::vector<int>& shape);
|
|
78
|
+
ValueVec instruction(const std::string& name, const ValueVec& args);
|
|
79
|
+
ValueVec instruction(InstructionPtr instruction, const ValueVec& args);
|
|
80
|
+
Function function();
|
|
81
|
+
|
|
82
|
+
Value sum(const ValueVec& values);
|
|
83
|
+
Value product(const ValueVec& values);
|
|
84
|
+
|
|
85
|
+
#include "function_builder_mixin.h"
|
|
86
|
+
|
|
87
|
+
private:
|
|
88
|
+
std::vector<Type> output_types;
|
|
89
|
+
ValueVec inputs;
|
|
90
|
+
std::vector<std::optional<Value>> outputs;
|
|
91
|
+
std::map<LiteralValue, Value> literals;
|
|
92
|
+
ValueVec locals;
|
|
93
|
+
std::unordered_map<std::string, Value> globals;
|
|
94
|
+
std::vector<InstructionCall> instructions;
|
|
95
|
+
std::map<std::vector<std::size_t>, std::vector<std::size_t>> instruction_cache;
|
|
96
|
+
std::vector<int> local_sources;
|
|
97
|
+
std::vector<bool> instruction_used;
|
|
98
|
+
|
|
99
|
+
void register_local(Value& val);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
} // namespace madspace
|