-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmexport.py
More file actions
263 lines (206 loc) · 7.98 KB
/
Copy pathmexport.py
File metadata and controls
263 lines (206 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
"""
Utilities for exporting a torch program to mindspore.
"""
import copy
from typing import Any, Dict, Tuple
import torch
from torch.utils import _pytree as pytree
import torch4ms
from torch4ms import tensor
from torch4ms import ops_registry, mappings
from torch4ms import decompositions
import mindspore as ms
import mindspore.numpy as mnp
DEBUG = False
class MsInterpreter(torch.fx.Interpreter):
"""
Interpreter that runs a torch.fx.GraphModule using MindSpore operations.
"""
def __init__(self, graph_module):
super().__init__(graph_module)
# Import MindSpore ops modules
import torch4ms.ops.maten
import torch4ms.ops.mtorch
def call_function(self, target, args: Tuple, kwargs: Dict) -> Any:
if not isinstance(target,
(torch._ops.OpOverloadPacket, torch._ops.OpOverload)):
return super().call_function(target, args, kwargs)
if DEBUG:
print('Running ', target.name(), '--------')
# Get the operator from registry
op = ops_registry.all_aten_ops.get(target)
if op is None:
op = ops_registry.all_aten_ops.get(target.overloadpacket)
assert op is not None, f"No operator found for {target}"
# Use the function directly (we've already registered MindSpore implementations)
return op.func(*args, **kwargs)
def run_node(self, n) -> Any:
res = super().run_node(n)
if DEBUG:
if n.op == 'call_function':
if hasattr(res, 'shape'):
print('Meta:', n.meta.get('val').shape, 'REAL: ', res.shape)
return res
from torch._decomp import get_decompositions
import torch._refs
_extra_decomp = get_decompositions([torch.ops.aten.unfold])
def _extract_states_from_exported_program(exported_model):
"""
Extract parameters and buffers from an exported program.
Args:
exported_model: Exported PyTorch model
Returns:
Tuple of (parameter/buffer names, parameter/buffer values)
"""
# NOTE call convention: (parameters, buffers, user_inputs)
param_and_buffer_keys = exported_model.graph_signature.parameters + exported_model.graph_signature.buffers
state_dict = copy.copy(exported_model.state_dict)
if (constants := getattr(exported_model, 'constants', None)) is not None:
state_dict.update(constants)
param_buffer_values = list(state_dict[key] for key in param_and_buffer_keys)
if hasattr(exported_model.graph_signature, "lifted_tensor_constants"):
for name in exported_model.graph_signature.lifted_tensor_constants:
param_buffer_values.append(exported_model.tensor_constants[name])
return param_and_buffer_keys, param_buffer_values
def exported_program_to_ms(exported_program, export_raw: bool = False):
"""
Convert an exported PyTorch program to MindSpore.
Args:
exported_program: PyTorch exported program
export_raw: Whether to export raw values or convert to MindSpore tensors
Returns:
If export_raw is True: (names, states, func)
If export_raw is False: (states, func)
"""
if torch.__version__ >= '2.2':
# torch version 2.1 didn't expose this yet
exported_program = exported_program.run_decompositions()
exported_program = exported_program.run_decompositions(
decompositions.DECOMPOSITIONS)
if DEBUG:
print(exported_program.graph_module.code)
names, states = _extract_states_from_exported_program(exported_program)
def _extract_args(args, kwargs):
flat_args, received_spec = pytree.tree_flatten(
(args, kwargs)) # type: ignore[possibly-undefined]
return flat_args
num_mutations = len(exported_program.graph_signature.buffers_to_mutate)
def func(states, inputs):
args = _extract_args(inputs, {})
res = MsInterpreter(exported_program.graph_module).run(
*states,
*args,
enable_io_processing=False,
)
res = res[num_mutations:]
return res
if export_raw:
return names, states, func
env = torch4ms.default_env()
states = env.t2ms_copy(states)
return states, func
def extract_tensor_types(exported):
"""
Return MindSpore tensor types for all input parameters of the exported program.
This supports dynamic batch dimensions.
Args:
exported: Exported PyTorch program
Returns:
List of MindSpore tensor type descriptions
"""
def _to_tensor_type(arg_meta):
"""
Convert from torch type to MindSpore tensor type description
"""
val = arg_meta['val']
is_scalar = isinstance(val, float) or isinstance(val, int) or isinstance(
val, bool)
if is_scalar:
return {'shape': (), 'dtype': type(arg_meta['val'])}
tensor_meta = arg_meta['tensor_meta']
shape = list(tensor_meta.shape)
dtype = mappings.t2ms_dtype(tensor_meta.dtype)
return {'shape': shape, 'dtype': dtype}
def _get_inputs(exported):
"""
Return placeholders with input metadata
"""
placeholders = [p for p in exported.graph.nodes if p.op == "placeholder"]
input_placeholders = [
p for p, s in zip(placeholders, exported.graph_signature.input_specs)
if s.kind == torch.export.graph_signature.InputKind.USER_INPUT
]
return input_placeholders
args = _get_inputs(exported)
if DEBUG:
print('Inputs to tensor type:', args, '--------')
for arg in args:
print('Meta2TensorType', arg.meta, '--> ', _to_tensor_type(arg.meta))
return [_to_tensor_type(arg.meta) for arg in args]
def create_ms_model_from_exported_program(exported_program):
"""
Create a MindSpore model from an exported PyTorch program.
Args:
exported_program: PyTorch exported program
Returns:
Tuple of (weights, ms_model) where weights is a dictionary of parameters
and ms_model is a callable MindSpore model
"""
weights, func = exported_program_to_ms(exported_program)
tensor_types = extract_tensor_types(exported_program)
# Create a simple MindSpore model that wraps the function
class WrappedModel:
def __init__(self, weights, func):
self.weights = weights
self.func = func
def __call__(self, *inputs):
# Combine inputs into a tuple
inputs_tuple = inputs if len(inputs) > 1 else inputs[0]
return self.func(self.weights, inputs_tuple)
ms_model = WrappedModel(weights, func)
return weights, ms_model
def export_to_ms_script(exported_program, filename=None):
"""
Export an exported PyTorch program to MindSpore script.
Args:
exported_program: PyTorch exported program
filename: Optional filename to save the script to
Returns:
Generated MindSpore script as a string
"""
# Extract information from the exported program
names, states, func = exported_program_to_ms(exported_program, export_raw=True)
tensor_types = extract_tensor_types(exported_program)
# Generate script code
script = [
"import mindspore as ms",
"import mindspore.numpy as mnp",
"from mindspore import ops",
"",
"# Model generated from torch.export",
""
]
# Add parameter definitions
script.append("# Model parameters")
for name, state in zip(names, states):
if isinstance(state, torch.Tensor):
# Convert PyTorch tensor to MindSpore tensor initialization code
shape_str = str(tuple(state.shape))
dtype_str = mappings.t2ms_dtype(state.dtype).__name__
# For simplicity, we'll just create a tensor with zeros of the same shape and dtype
# In a real implementation, you would save the actual weights
script.append(f"{name} = mnp.zeros({shape_str}, dtype={dtype_str})")
# Add model function
script.append("\n# Model function")
script.append("def model_function(parameters, inputs):")
script.append(" # This is a placeholder for the actual model implementation")
script.append(" # In a real implementation, this would contain the translated operations")
script.append(" return inputs") # Simple placeholder
# Combine script lines
script_str = '\n'.join(script)
# Save to file if filename is provided
if filename:
with open(filename, 'w') as f:
f.write(script_str)
print(f"Model script saved to {filename}")
return script_str