Code Sample
MindSpore Training Script Sample in the Ascend NPU Environment
import mindspore as ms
import numpy as np
import mindspore
mindspore.set_device("Ascend")
import mindspore.mint as mint
import mindspore.dataset as ds
from mindspore import nn
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.fc = nn.Dense(2, 2)
def construct(self, x):
# Pass through the fully connected layers
y = self.fc(x)
# Call mint.add to calculate the sum of y and self.
# You can also change it to mint.add(x, x) or mint.add(y, x) as required.
z = mint.add(y, y)
return z
def generator_net():
for _ in range(10):
yield np.ones([2, 2]).astype(np.float32), np.ones([2]).astype(np.int32)
def forward_fn(data, label):
logits = model(data)
loss = loss_fn(logits, label)
return loss, logits
model = Net()
optimizer = nn.Momentum(model.trainable_params(), 1, 0.9)
loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
grad_fn = mindspore.value_and_grad(forward_fn, None, optimizer.parameters, has_aux=True)
# Define the single-step training function.
def train_step(data, label):
(loss, _), grads = grad_fn(data, label)
optimizer(grads)
return loss
if __name__ == "__main__":
step = 0
# Train the model.
for data, label in ds.GeneratorDataset(generator_net(), ["data", "label"]):
train_step(data, label)
print(f"train step {step}")
step += 1
print("train finish")
Code Sample for MindSpore Pre-Training Configuration Check
from msprobe.core.config_check import ConfigChecker
ConfigChecker.apply_patches(fmk="mindspore")
import mindspore as ms
import numpy as np
import mindspore
mindspore.set_device("Ascend")
import mindspore.mint as mint
import mindspore.dataset as ds
from mindspore import nn
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.fc = nn.Dense(2, 2)
def construct(self, x):
# Pass through the fully connected layers
y = self.fc(x)
# Call mint.add to calculate the sum of y and self.
# You can also change it to mint.add(x, x) or mint.add(y, x) as required.
z = mint.add(y, y)
return z
def generator_net():
for _ in range(10):
yield np.ones([2, 2]).astype(np.float32), np.ones([2]).astype(np.int32)
def forward_fn(data, label):
logits = model(data)
loss = loss_fn(logits, label)
return loss, logits
model = Net()
optimizer = nn.Momentum(model.trainable_params(), 1, 0.9)
loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
grad_fn = mindspore.value_and_grad(forward_fn, None, optimizer.parameters, has_aux=True)
# Define the single-step training function.
def train_step(data, label):
(loss, _), grads = grad_fn(data, label)
optimizer(grads)
return loss
if __name__ == "__main__":
step = 0
from msprobe.core.config_check import ConfigChecker
ConfigChecker(model=model, output_zip_path="./config_check_pack.zip", fmk="mindspore")
# Train the model.
for data, label in ds.GeneratorDataset(generator_net(), ["data", "label"]):
train_step(data, label)
print(f"train step {step}")
step += 1
print("train finish")
Code Sample for MindSpore Accuracy Data Collection
import mindspore as ms
import numpy as np
import mindspore
mindspore.set_device("Ascend")
import mindspore.mint as mint
import mindspore.dataset as ds
from mindspore import nn
from msprobe.mindspore import PrecisionDebugger
debugger = PrecisionDebugger(config_path="./config.json")
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.fc = nn.Dense(2, 2)
def construct(self, x):
# Pass through the fully connected layers
y = self.fc(x)
# Call mint.add to calculate the sum of y and self.
# You can also change it to mint.add(x, x) or mint.add(y, x) as required.
z = mint.add(y, y)
return z
def generator_net():
for _ in range(10):
yield np.ones([2, 2]).astype(np.float32), np.ones([2]).astype(np.int32)
def forward_fn(data, label):
logits = model(data)
loss = loss_fn(logits, label)
return loss, logits
model = Net()
optimizer = nn.Momentum(model.trainable_params(), 1, 0.9)
loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
grad_fn = mindspore.value_and_grad(forward_fn, None, optimizer.parameters, has_aux=True)
# Define the single-step training function.
def train_step(data, label):
(loss, _), grads = grad_fn(data, label)
optimizer(grads)
return loss
if __name__ == "__main__":
step = 0
# Train the model.
for data, label in ds.GeneratorDataset(generator_net(), ["data", "label"]):
debugger.start(model)
train_step(data, label)
print(f"train step {step}")
step += 1
debugger.stop()
debugger.step()
print("train finish")
Code Sample for Using MindSpore Profiler APIs to Collect Profile Data
import mindspore as ms
import numpy as np
import mindspore
mindspore.set_device("Ascend")
import mindspore.mint as mint
import mindspore.dataset as ds
from mindspore import nn
from mindspore.profiler import ProfilerLevel, ProfilerActivity, AicoreMetrics
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.fc = nn.Dense(2, 2)
def construct(self, x):
# Pass through the fully connected layers
y = self.fc(x)
# Call mint.add to calculate the sum of y and self.
# You can also change it to mint.add(x, x) or mint.add(y, x) as required.
z = mint.add(y, y)
return z
def generator_net():
for _ in range(10):
yield np.ones([2, 2]).astype(np.float32), np.ones([2]).astype(np.int32)
def forward_fn(data, label):
logits = model(data)
loss = loss_fn(logits, label)
return loss, logits
model = Net()
optimizer = nn.Momentum(model.trainable_params(), 1, 0.9)
loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
grad_fn = mindspore.value_and_grad(forward_fn, None, optimizer.parameters, has_aux=True)
# Define the single-step training function.
def train_step(data, label):
(loss, _), grads = grad_fn(data, label)
optimizer(grads)
return loss
if __name__ == "__main__":
mindspore.set_context(mode=mindspore.PYNATIVE_MODE)
mindspore.set_device("Ascend")
# Init Profiler
experimental_config = mindspore.profiler._ExperimentalConfig(
profiler_level=ProfilerLevel.Level0,
aic_metrics=AicoreMetrics.AiCoreNone,
l2_cache=False,
mstx=False,
data_simplification=False
)
step = 0
# Note that the Profiler should be initialized before model.train
with mindspore.profiler.profile(
activities=[ProfilerActivity.CPU, ProfilerActivity.NPU],
schedule=mindspore.profiler.schedule(
wait=0, warmup=0, active=1, repeat=1, skip_first=0
),
on_trace_ready=mindspore.profiler.tensorboard_trace_handler("./profiling_data"),
profile_memory=False,
experimental_config=experimental_config,
) as prof:
# Train the model.
for data, label in ds.GeneratorDataset(generator_net(), ["data", "label"]):
train_step(data, label)
print(f"train step {step}")
step += 1
prof.step()
print("train finish")
Parent topic: Training Tools Quick Start