AI Development Framework · Topic 1 of 2

AI Frameworks and Development Basics

Learn what an AI framework provides, PyTorch, TensorFlow and MindSpore, tensors, static and dynamic graphs, autograd and GPU or NPU backends for H13-311.

  • 5 min
In a nutshell
  • A framework provides tensor computation, automatic differentiation, model building blocks, hardware acceleration, distributed training and deployment.
  • PyTorch (Meta), TensorFlow (Google), MindSpore (Huawei), PaddlePaddle (Baidu).
  • A tensor has a shape and a dtype; image batches in PyTorch are NCHW.

What it is

An AI framework (deep learning framework) is a software library that lets you build, train and deploy neural networks without writing the math and hardware code yourself. You describe the model in a few lines, and the framework handles tensor storage, gradients and the GPU or NPU.

Without a framework, every layer, every derivative and every device kernel would have to be written by hand. Frameworks make deep learning fast to develop and easy to reuse.

Developer codemodel and dataAI frameworktensors, graph, autogradOperator kernelsHardwareCPU, GPU, NPU

How it works

What a framework provides

CapabilityWhat it gives you
Tensor computationFast multidimensional arrays and math operations
Automatic differentiationGradients computed for you, so backpropagation is automatic
Model building blocksReady layers, activation functions, loss functions and optimizers
Hardware accelerationThe same code runs on CPU, GPU or NPU
Distributed trainingTraining split across many devices or servers
Deployment toolsExporting and running trained models for inference

Mainstream frameworks

FrameworkDeveloperNotes
PyTorchMeta, now under the PyTorch FoundationDynamic graphs, Pythonic style, dominant in research; used in the HCIA-AI V4.0 course
TensorFlowGoogleKeras as its high level API; eager execution by default since TensorFlow 2
MindSporeHuaweiOpen source, all scenario (device, edge, cloud), native support for Ascend processors
PaddlePaddleBaiduOpen source framework popular in China

All of them follow the same basic ideas, so knowing one makes the others easy to learn.

Layers of a framework

A framework is usually organized in layers.

The front end is the Python API you call. The graph layer represents and optimizes the computation. The operator layer holds kernels, the hardware specific code for each operation such as matrix multiplication or convolution.

At the bottom are hardware libraries. NVIDIA GPUs use CUDA, and Huawei Ascend NPUs use CANN (Compute Architecture for Neural Networks), Huawei's heterogeneous computing architecture for Ascend.

Tensors

A tensor is the basic data structure: a multidimensional array with a shape and a data type (dtype). The number of dimensions is its rank, or order.

RankNameExample
0ScalarA loss value, such as 0.25
1VectorA list of 10 class scores
2MatrixA weight matrix of shape (784, 128)
44D tensorA batch of images

PyTorch stores image batches as NCHW: batch size, channels, height, width. So 32 RGB images of 224 x 224 pixels form a tensor of shape (32, 3, 224, 224). The default floating point dtype is float32.

Computational graphs

A computational graph is a directed graph whose nodes are operations and whose edges carry tensors. The framework uses it to run the forward pass and to work out gradients in the backward pass.

Graph typeHow it worksStrengthWeakness
Dynamic graph (define by run)Built step by step as the code executesEasy to write and debug with normal PythonFewer global optimizations
Static graph (define then run)Whole graph built first, then compiled and executedBetter optimization and deploymentHarder to debug

PyTorch is dynamic by default; torch.compile in PyTorch 2 can compile code into optimized graphs. TensorFlow 2 runs eagerly but turns functions into graphs with tf.function. MindSpore offers both a graph mode and a PyNative (dynamic) mode.

Automatic differentiation

Training needs the gradient of the loss with respect to every weight. Automatic differentiation (autograd) records each operation in the forward pass, then applies the chain rule backward through the graph. This is reverse mode differentiation, which is what backpropagation is.

Inputs and weightsforward passLossbackward passGradientsoptimizerUpdated weights

A worked example

Take y = x squared + 3x at x = 2. By hand, dy/dx = 2x + 3 = 2(2) + 3 = 7. PyTorch gets the same result automatically:

import torch
x = torch.tensor(2.0, requires_grad=True)
y = x**2 + 3*x      # forward pass builds the graph
y.backward()        # reverse mode autograd
print(x.grad)       # tensor(7.)

requires_grad=True tells PyTorch to track operations on x. After backward(), the gradient is stored in x.grad.

Moving work to a GPU is a single call such as x.to("cuda"). The model code does not change.

How the exam asks about this

  • "Which of the following is NOT a main function of an AI framework?" → Anything outside tensors, autograd, model building, acceleration, distributed training and deployment, such as collecting the raw business data.
  • "Which framework is developed by Huawei and supports device, edge and cloud scenarios?" → MindSpore.
  • "PyTorch builds its computational graph while the code runs. True or false?" → True; it uses a dynamic graph.

What to remember for the exam

  • A framework provides tensor computation, automatic differentiation, model building blocks, hardware acceleration, distributed training and deployment.
  • PyTorch (Meta), TensorFlow (Google), MindSpore (Huawei), PaddlePaddle (Baidu).
  • A tensor has a shape and a dtype; image batches in PyTorch are NCHW.
  • Dynamic graphs are built as code runs and are easy to debug; static graphs are built first and optimize better.
  • Autograd applies the chain rule backward through the graph, which is backpropagation.
Watch out

Autograd computes gradients; it does not update the weights. Updating is the optimizer's job, done in a separate step after the gradients exist.

Common mistakes

Treating a tensor as just a matrix. A matrix is only a rank 2 tensor. Tensors can have any number of dimensions, from a scalar (rank 0) to a 4D image batch and beyond.

Saying static graphs are always faster to develop. Static graphs usually run and deploy more efficiently, but dynamic graphs are faster to write and debug. The trade off is development ease versus optimization.

Confusing CANN with a training framework. CANN is the computing architecture that drives Ascend hardware. Frameworks such as MindSpore or PyTorch run on top of it.

Check yourself

  • What does automatic differentiation do in a framework?

    It records the operations of the forward pass and applies the chain rule backward to compute the gradient of the loss with respect to every parameter.

  • What shape does PyTorch use for a batch of 16 grayscale 28 x 28 images?

    (16, 1, 28, 28), in NCHW order: batch size, channels, height, width.

  • What is the main advantage of a dynamic computational graph?

    It is built as the code runs, so the model can use normal Python control flow and is easy to debug step by step.

Topic 1 of 2 in AI Development Framework

Test yourself on AI Development Framework

The full Huawei HCIA-AI H13-311 question bank has 600 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Huawei. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.