Original author: jaypume
Original lecture video: https://www.bilibili.com/video/BV1hg4y1b78L
Original post: https://github.com/jaypume/article/blob/main/sedna/边云协同AI框架Sedna源码解析/README.MD
Reposted here for my own study, easier to look up.
KubeEdge-Sedna overview
Sedna is a cloud–edge collaborative AI project incubated in KubeEdge SIG AI. On top of KubeEdge’s cloud–edge abilities, Sedna can do collaborative training and inference across cloud and edge: joint inference, incremental learning, federated learning, lifelong learning, etc. It supports common AI frameworks — TensorFlow / Pytorch / MindSpore — so existing AI apps can move over and pick up cloud–edge train/infer, with gains on cost, model quality, and data privacy.
Project home:
https://github.com/kubeedge/sedna
Docs:
Architecture
Sedna’s cloud–edge collab sits on these KubeEdge pieces:
- Unified orchestration of apps across cloud and edge
- Router: reliable control-plane message channel cloud–edge
- EdgeMesh: data-plane service discovery and traffic across cloud and edge

Basic components:
- GlobalManager
- Unified management of cloud–edge AI jobs
- Cross cloud–edge coordination
- Central config
- LocalController
- Local flow control of cloud–edge AI jobs
- Local generic management: models, datasets, status sync, etc.
- Lib
- For AI and app developers: expose cloud–edge AI features to the app
- Worker
- Run train or infer, programs built on existing AI frameworks
- Different features → different worker groups; workers can sit on edge or cloud and collaborate
Repo layout
| Dir | What |
|---|---|
| .github | Sedna GitHub CI/CD |
| LICENSES | Sedna licenses and vendor licenses |
| build | Dockerfiles for GM/LC; generated CRD yaml; CRD samples |
| cmd | GM/LC entrypoints |
| components | Monitoring and UI |
| docs | Proposals and install docs |
| examples | Joint infer, incremental, lifelong, federated samples |
| hack | Codegen and other dev scripts |
| lib | Sedna Library, Python deps for cloud–edge AI apps |
| pkg | API defs; generated client-go; GM/LC core |
| scripts | Install scripts for users |
| test | E2E tests and tools |
| vendor | Third-party source |
Sedna control plane source (Go)
GM: Global Manager
GM, a Kubernetes operator
What’s an operator?
An Operator is an application-specific controller that extends the Kubernetes API to create, configure and manage instances of complex stateful applications on behalf of a Kubernetes user. It builds upon the basic Kubernetes resource and controller concepts, but also includes domain or application-specific knowledge to automate common tasks better managed by computers. 1
For Sedna, GM decides how workers are configured and started, how they collaborate, how jobs move between stages. So: Sedna GM is the controller for the domain “cloud–edge collaborative AI apps”.
The following components form the three main parts of an operator:
- API: The data that describes the operand’s configuration. The API includes:
- Custom resource definition (CRD), which defines a schema of settings available for configuring the operand.
- Programmatic API, which defines the same data schema as the CRD and is implemented using the operator’s programming language, such as Go.
- Custom resource (CR), which specifies values for the settings defined by the CRD; these values describe the configuration of an operand.
- Controller: The brains of the operator. The controller creates managed resources based on the description in the custom resource; controllers are implemented using the operator’s programming language, such as Go. 2
From that Red Hat definition, the important operator pieces are CRD, API, CR, and Controller.
Sedna GM Operator sketch:

The next sections follow those pieces: CR, CRD, API, Controller. Controller is the main logic.
CR
Sedna supports joint inference, incremental learning, lifelong learning, federated learning. To keep the walkthrough tractable this post uses lifelong learning. The other three share a lot; you can map them.
CR sample
A lifelong-learning CR sample. You can kubectl-create from it. Full steps here. Fields:
- dataset: dataset object name; the dataset is also a CR.
- trainSpec: train worker start params — image, env, etc.
- trigger: when to start the train worker.
- evalSpec: eval worker start params.
- deploySpec: infer worker start params.
- outputDir: where trained models go.
build/crd-samples/sedna/lifelonglearningjobv1alpha1.yaml
apiVersion: sedna.io/v1alpha1
kind: LifelongLearningJob
metadata:
name: atcii-classifier-demo
spec:
dataset:
name: "lifelong-dataset"
trainProb: 0.8
trainSpec:
template:
spec:
nodeName: "edge-node"
containers:
- image: kubeedge/sedna-example-lifelong-learning-atcii-classifier:v0.3.0
name: train-worker
imagePullPolicy: IfNotPresent
args: ["train.py"]
env:
- name: "early_stopping_rounds"
value: "100"
- name: "metric_name"
value: "mlogloss"
trigger:
checkPeriodSeconds: 60
timer:
start: 02:00
end: 24:00
condition:
operator: ">"
threshold: 500
metric: num_of_samples
evalSpec:
template:
spec:
nodeName: "edge-node"
containers:
- image: kubeedge/sedna-example-lifelong-learning-atcii-classifier:v0.3.0
name: eval-worker
imagePullPolicy: IfNotPresent
args: ["eval.py"]
env:
- name: "metrics"
value: "precision_score"
- name: "metric_param"
value: "{'average': 'micro'}"
- name: "model_threshold"
value: "0.5"
deploySpec:
template:
spec:
nodeName: "edge-node"
containers:
- image: kubeedge/sedna-example-lifelong-learning-atcii-classifier:v0.3.0
name: infer-worker
imagePullPolicy: IfNotPresent
args: ["inference.py"]
env:
- name: "UT_SAVED_URL"
value: "/ut_saved_url"
- name: "infer_dataset_url"
value: "/data/testData.csv"
volumeMounts:
- name: utdir
mountPath: /ut_saved_url
- name: inferdata
mountPath: /data/
resources:
limits:
memory: 2Gi
volumes:
- name: utdir
hostPath:
path: /lifelong/unseen_task/
type: DirectoryOrCreate
- name: inferdata
hostPath:
path: /data/
type: DirectoryOrCreate
outputDir: "/output"CRD
A CRD is the template for a CR. You have to declare the CRD on the cluster before you can create CRs. YAML can be written by hand or generated; for non-trivial CRDs, generate. Sedna uses kubebuilder’s controller-gen. make crds updates files under build/crds/. See crds: controller-gen in the Makefile.
To define a CRD you need group, version, kind — GVK. The object itself is a Resource. In OO terms Resource ≈ Object, Kind ≈ Class, so a Resource is an instance of a Kind. Lifelong learning GVR / GVK:
| Group | Version | Resource | Kind | |
|---|---|---|---|---|
| CRD | apiextensions.k8s.io | v1 | lifelonglearningjobs.sedna.io | CustomResourceDefinition |
| CR | sedna.io | v1alpha1 | lifelonglearningjob | LifelongLearningJob |
In K8s, resources are organized as REST URIs:

Once you know the pattern you can paste a REST URI and hit the cluster without kubectl / client-go. For example:
Lifelong-learning CRD via REST:
curl -k --cert ./client.crt --key ./client.key https://127.0.0.1:5443/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions/lifelonglearningjobs.sedna.ioLifelong-learning CR list via REST:
curl -k --cert ./client.crt --key ./client.key https://127.0.0.1:5443/apis/sedna.io/v1alpha1/lifelonglearningjobsIf a language has no official k8s client SDK, wrap these REST calls.
Sedna lifelong-learning CRD, fields to notice:
apiVersion: apiextensions.k8s.io/v1, all CRDs currently extend apiextensions.k8s.io/v1kind: CustomResourceDefinition, all CRDs inherit this Kindspec.group: sedna.io, custom resource group name is sedna.iospec.names.kind: LifelongLearningJob, the new typespec.names.shortNames: - ll, kubectl short namell
build/crds/sedna.io_lifelonglearningjobs.yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
creationTimestamp: null
name: lifelonglearningjobs.sedna.io
spec:
group: sedna.io
names:
kind: LifelongLearningJob
listKind: LifelongLearningJobList
plural: lifelonglearningjobs
shortNames:
- ll
singular: lifelonglearningjob
scope: Namespaced
versions:
- name: v1alpha1
...
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []API
CRDs are generated — where’s the API that they come from?
pkg/apis/sedna/v1alpha1/lifelonglearningjob_types.go
package v1alpha1
import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// 这里展示了
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:shortName=ll
// +kubebuilder:subresource:status
// 整体的LifelongLearningJob的API定义,主要包含Spec和Status定义,分别代表期望状态和实际状态。
type LifelongLearningJob struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata"`
Spec LLJobSpec `json:"spec"`
Status LLJobStatus `json:"status,omitempty"`
}
// 在创建LifelongLearningJob时候需要配置的参数;如果需要扩展终身学习字段的接口,可以在这里修改。
type LLJobSpec struct {
Dataset LLDataset `json:"dataset"`
TrainSpec LLTrainSpec `json:"trainSpec"`
EvalSpec LLEvalSpec `json:"evalSpec"`
DeploySpec LLDeploySpec `json:"deploySpec"`
// the credential referer for OutputDir
CredentialName string `json:"credentialName,omitempty"`
OutputDir string `json:"outputDir"`
}
type LLDataset struct {
Name string `json:"name"`
TrainProb float64 `json:"trainProb"`
}
// 剩下还有一些结构体定义省略了。Notes on that snippet:
// +kubebuilder...: flags for kubebuilder / codegentype LifelongLearningJob struct{...}: overall API, Spec (desired) and Status (actual)type LLJobSpec struct {...}: fields you set when creating the CR; extend lifelong-learning here
Joint infer / incremental / federated APIs live under pkg/apis/sedna/v1alpha1/ too.
Update client-go
After changing *_types.go:
bash hack/update-codegen.shGenerated code is in pkg/client:
➜ pkg tree client -L 2
client
├── clientset
│ └── versioned
├── informers
│ └── externalversions
└── listers
└── sednaclient-go is used later in the Controller.
Update CRD defs
After changing *_types.go:
make crdsYAML lands in build/crds. Then kubectl apply again so the cluster picks up the new CRD.
Controller
Main lifelong-learning control is pkg/globalmanager/controllers/lifelonglearning/lifelonglearningjob.go — when train/eval workers fire, how params sync to the edge, etc.
Call flow before that logic, as pseudocode:
cmd/sedna-gm/sedna-gm.go/main() 【1】
pkg/globalmanager/controllers/manager.go/New() 【2】读取GM配置文件。
pkg/globalmanager/controllers/manager.go/Start() 【3】启动GM进程。
- clientset.NewForConfig():【4】调用client-go生成了Sedna CRD client。
- NewUpstreamController():【5】创建UpstreamController,每个GM进程有一个UpstreamController
- uc.Run(stopCh):启动一个for循环协程,来处理
- pkg/globalmanager/controllers/upstream.go/syncEdgeUpdate()
- NewRegistry():【6】注册所有controller。
- f.SetDownstreamSendFunc()【7】
-> pkg/globalmanager/controllers/lifelonglearning/downstream.go
- f.SetUpstreamHandler()【8】
-> pkg/globalmanager/controllers/lifelonglearning/upstream.go/updateFromEdge()
- f.Run()【9】
- ws.ListenAndServe() 【10】LifelongLearningJob Controller, following labels 1–10 above:
[1] main
sedna-gm.go is the GM entry: logs, app.NewControllerCommand() parses flags, starts the GM controller.
cmd/sedna-gm/sedna-gm.go
func main() {
rand.Seed(time.Now().UnixNano())
command := app.NewControllerCommand()
logs.InitLogs()
defer logs.FlushLogs()
if err := command.Execute(); err != nil {
os.Exit(1)
}
}[2] GM config
GM loads config: K8s cluster, websocket listen addr/port, KB service addr, etc.
pkg/globalmanager/controllers/manager.go
// New creates the controller manager
func New(cc *config.ControllerConfig) *Manager {
config.InitConfigure(cc)
return &Manager{
Config: cc,
}
}pkg/globalmanager/config/config.go
// ControllerConfig indicates the config of controller
type ControllerConfig struct {
// KubeAPIConfig indicates the kubernetes cluster info which controller will connected
KubeConfig string `json:"kubeConfig,omitempty"`
// Master indicates the address of the Kubernetes API server. Overrides any value in KubeConfig.
// such as https://127.0.0.1:8443
// default ""
Master string `json:"master"`
// Namespace indicates which namespace the controller listening to.
// default ""
Namespace string `json:"namespace,omitempty"`
// websocket server config
// Since the current limit of kubeedge(1.5), GM needs to build the websocket channel for communicating between GM and LCs.
WebSocket WebSocket `json:"websocket,omitempty"`
// lc config to info the worker
LC LCConfig `json:"localController,omitempty"`
// kb config to info the worker
KB KBConfig `json:"knowledgeBaseServer,omitempty"`
// period config min resync period
// default 30s
MinResyncPeriodSeconds int64 `json:"minResyncPeriodSeconds,omitempty"`
}[3] GM init
Init Sedna CRD client, bind and start cloud–edge message handlers, start per-feature controllers, start websocket listen.
pkg/globalmanager/controllers/manager.go
// Start starts the controllers it has managed
func (m *Manager) Start() error {
...
// 初始化Sedna CRD client,Controller会监听Sedna CR 增删改查的变化,并执行对应的处理逻辑。
sednaClient, err := clientset.NewForConfig(kubecfg)
...
sednaInformerFactory := sednainformers.NewSharedInformerFactoryWithOptions(sednaClient, genResyncPeriod(minResyncPeriod), sednainformers.WithNamespace(namespace))
// 初始化UpstreamController,用于处理边缘LC上传的消息
uc, _ := NewUpstreamController(context)
downstreamSendFunc := messagelayer.NewContextMessageLayer().SendResourceObject
stopCh := make(chan struct{})
go uc.Run(stopCh)
// 针对每个特性(协同推理、终身学习等),绑定对应的消息处理函数
for name, factory := range NewRegistry() {
...
f.SetDownstreamSendFunc(downstreamSendFunc)
f.SetUpstreamHandler(uc.Add)
...
// 启动各个特性对应controller
go f.Run(stopCh)
}
...
// 启动整体GM的websocket,默认监听在0.0.0.0:9000这个端口地址
ws := websocket.NewServer(addr)
...
}[4] CRD client init
clientset.NewForConfig() lives in pkg/client/clientset/versioned/clientset.go — generated by client-go from the CRD, Go CRUD on those objects.
LifelongLearningJob Controller init uses that client. It:
- Gets the LifelongLearningJob Informer. Informer ≈ local cache of the api-server for this controller, to ease read pressure.
- Sets controller fields: k8s client, sedna client, shared GM config.
- Binds Add / Update / Delete callbacks on the CR.
pkg/globalmanager/controllers/lifelonglearning/lifelonglearningjob.go
// New creates a new LifelongLearningJob controller that keeps the relevant pods
// in sync with their corresponding LifelongLearningJob objects.
func New(cc *runtime.ControllerContext) (runtime.FeatureControllerI, error) {
cfg := cc.Config
podInformer := cc.KubeInformerFactory.Core().V1().Pods()
// 获取LifelongLearningJob的Informer
jobInformer := cc.SednaInformerFactory.Sedna().V1alpha1().LifelongLearningJobs()
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: cc.KubeClient.CoreV1().Events("")})
// 配置LifelongLearningJob Controller的参数
jc := &Controller{
kubeClient: cc.KubeClient,
client: cc.SednaClient.SednaV1alpha1(),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.NewItemExponentialFailureRateLimiter(runtime.DefaultBackOff, runtime.MaxBackOff), Name),
cfg: cfg,
}
// 绑定LifelongLearningJob CRD资源的Add、Update、Delete对应事件的回调函数。
jobInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
jc.enqueueController(obj, true)
jc.syncToEdge(watch.Added, obj)
},
UpdateFunc: func(old, cur interface{}) {
jc.enqueueController(cur, true)
jc.syncToEdge(watch.Added, cur)
},
DeleteFunc: func(obj interface{}) {
jc.enqueueController(obj, true)
jc.syncToEdge(watch.Deleted, obj)
},
})
jc.jobLister = jobInformer.Lister()
jc.jobStoreSynced = jobInformer.Informer().HasSynced
// 绑定Pod对应的增删改对应事件的回调函数。
podInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: jc.addPod,
UpdateFunc: jc.updatePod,
DeleteFunc: jc.deletePod,
})
jc.podStore = podInformer.Lister()
jc.podStoreSynced = podInformer.Informer().HasSynced
return jc, nil
}Sedna CRD client used from other modules too:

[5] Message handling init
uc.Run() inits UpstreamController, which handles all messages from the edge.
A for-loop listens on context.upstreamChannel. On a message, uc.updateHandlers[kind] picks the handler and calls it. The map holds handlers for joint infer, incremental, federated, lifelong.
pkg/globalmanager/controllers/upstream.go
// syncEdgeUpdate receives the updates from edge and syncs these to k8s.
func (uc *UpstreamController) syncEdgeUpdate() {
for {
select {
case <-uc.messageLayer.Done():
klog.Info("Stop sedna upstream loop")
return
default:
}
update, err := uc.messageLayer.ReceiveResourceUpdate()
...
handler, ok := uc.updateHandlers[kind]
if ok {
err := handler(name, namespace, operation, update.Content)
...
}
}
}ReceiveFromEdge is a blocking channel for messages from the edge LC, type nodeMessage.
pkg/globalmanager/messagelayer/ws/context.go
// ReceiveResourceUpdate receives and handles the update
func (cml *ContextMessageLayer) ReceiveResourceUpdate() (*ResourceUpdateSpec, error) {
nodeName, msg, err := wsContext.ReceiveFromEdge()
...
}[6] Controller registry
NewRegistry() registers every feature’s New. Adding a new cloud–edge feature means adding a New here.
pkg/globalmanager/controllers/registry.go
func NewRegistry() Registry {
return Registry{
ji.Name: ji.New,
fe.Name: fe.New,
fl.Name: fl.New,
il.Name: il.New,
ll.Name: ll.New,
reid.Name: reid.New,
va.Name: va.New,
dataset.Name: dataset.New,
objs.Name: objs.New,
}
}[7] Cloud → edge sync
f.SetDownstreamSendFunc() binds each feature’s syncToEdge().
For lifelong learning:
- Get the node named on the Dataset CR.
- Get train / eval / deploy node names from annotations.
- Depending on job stage, send to different nodes.
pkg/globalmanager/controllers/lifelonglearning/downstream.go
func (c *Controller) syncToEdge(eventType watch.EventType, obj interface{}) error {
// 获取到对应的数据集指定的节点(Dataset CRD对象中有一个字段记录了Node名称)
ds, err := c.client.Datasets(job.Namespace).Get(context.TODO(), dataName, metav1.GetOptions{})
// 获取到训练、评估、部署对应的节点名称
getAnnotationsNodeName := func(nodeName sednav1.LLJobStage) string {
return runtime.AnnotationsKeyPrefix + string(nodeName)
}
ann := job.GetAnnotations()
if ann != nil {
trainNodeName = ann[getAnnotationsNodeName(sednav1.LLJobTrain)]
evalNodeName = ann[getAnnotationsNodeName(sednav1.LLJobEval)]
deployNodeName = ann[getAnnotationsNodeName(sednav1.LLJobDeploy)]
}
...
// 根据LifelongLearningJob所处阶段不同,发送消息到不同的节点上
switch jobStage {
case sednav1.LLJobTrain:
doJobStageEvent(trainNodeName)
case sednav1.LLJobEval:
doJobStageEvent(evalNodeName)
case sednav1.LLJobDeploy:
doJobStageEvent(deployNodeName)
}
return nil
}[8] Edge → cloud sync
f.SetUpstreamHandler() binds each feature’s updateFromEdge().
For lifelong learning:
- Change overall LifelongLearningJob state from edge task completion.
- Write that state back to k8s, the CR Status.
- Parse the edge message (JSON). Example body:
Example GM receives:
{
"phase": "train",
"status": "completed",
"output": {
"models": [{
"classes": ["road", "fence"],
"current_metric": null,
"format": "pkl",
"metrics": null,
"url": "/output/train/1/index.pkl"
}],
"ownerInfo": null
}
}pkg/globalmanager/controllers/lifelonglearning/upstream.go
// updateFromEdge syncs the edge updates to k8s
func (c *Controller) updateFromEdge(name, namespace, operation string, content []byte) error {
var jobStatus struct {
Phase string `json:"phase"`
Status string `json:"status"`
}
// 把边缘消息结构体进行解析。
err := json.Unmarshal(content, &jobStatus)
...
cond := sednav1.LLJobCondition{
Status: v1.ConditionTrue,
LastHeartbeatTime: metav1.Now(),
LastTransitionTime: metav1.Now(),
Data: string(condDataBytes),
Message: "reported by lc",
}
// 根据不同的边缘节点任务状态实现,变更当前LifelongLearningJob的整体状态
switch strings.ToLower(jobStatus.Status) {
case "ready":
cond.Type = sednav1.LLJobStageCondReady
case "completed":
cond.Type = sednav1.LLJobStageCondCompleted
case "failed":
cond.Type = sednav1.LLJobStageCondFailed
case "waiting":
cond.Type = sednav1.LLJobStageCondWaiting
default:
return fmt.Errorf("invalid condition type: %v", jobStatus.Status)
}
// 将当前LifelongLearningJob的整体状态写回k8s,也就是LifelongLearningJob这个CR的Status字段。
err = c.appendStatusCondition(name, namespace, cond)
...
}[9] Controller core
f.run() runs each feature controller. LifelongLearningJob’s run():
WaitForNamedCacheSync until Pod and LifelongLearningJob are in the Informer. Then start N workers.
pkg/globalmanager/controllers/lifelonglearning/lifelonglearningjob.go
// Run starts the main goroutine responsible for watching and syncing jobs.
func (c *Controller) Run(stopCh <-chan struct{}) {
workers := 1
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()
klog.Infof("Starting %s controller", Name)
defer klog.Infof("Shutting down %s controller", Name)
if !cache.WaitForNamedCacheSync(Name, stopCh, c.podStoreSynced, c.jobStoreSynced) {
klog.Errorf("failed to wait for %s caches to sync", Name)
return
}
klog.Infof("Starting %s workers", Name)
for i := 0; i < workers; i++ {
go wait.Until(c.worker, time.Second, stopCh)
}
<-stopCh
}c.worker calls processNextWorkItem() on each job.
pkg/globalmanager/controllers/lifelonglearning/lifelonglearningjob.go
// worker runs a worker thread that just dequeues items, processes them, and marks them done.
// It enforces that the syncHandler is never invoked concurrently with the same key.
func (c *Controller) worker() {
for c.processNextWorkItem() {
}
}processNextWorkItem() calls c.sync() for feature logic.
pkg/globalmanager/controllers/lifelonglearning/lifelonglearningjob.go
func (c *Controller) sync(key string) (bool, error) {
//省略了部分代码
ns, name, err := cache.SplitMetaNamespaceKey(key)
sharedJob, err := c.jobLister.LifelongLearningJobs(ns).Get(name)
// if job was finished previously, we don't want to redo the termination
if IsJobFinished(&job) {
return true, nil
}
// transit this job's state machine
needUpdated, err = c.transitJobState(&job)
if needUpdated {
if err := c.updateJobStatus(&job); err != nil {
return forget, err
}
if jobFailed && !IsJobFinished(&job) {
// returning an error will re-enqueue LifelongLearningJob after the backoff period
return forget, fmt.Errorf("failed pod(s) detected for lifelonglearningjob key %q", key)
}
forget = true
}
return forget, err
}sync on a concrete job:
SplitMetaNamespaceKey→ namespace and namec.jobListerfetches the objecttransitJobStatedecides train / eval / deploy- If Status changed,
c.updateJobStatus()writes back so kubectl shows current stage, model path, etc. - Failure handling
// transit this job's state machine
needUpdated, err = c.transitJobState(&job)transitJobState() is the state machine: when train / eval / deploy start and stop. See the diagram:

[10] websocket listen
A websocket for the edge messages from [8], default 0.0.0.0:9000.
pkg/globalmanager/controllers/manager.go
addr := fmt.Sprintf("%s:%d", m.Config.WebSocket.Address, m.Config.WebSocket.Port)
ws := websocket.NewServer(addr)
err = ws.ListenAndServe()LC: Local Controller
LC runs on the edge: local job management and a message proxy. Entry cmd/sedna-lc/sedna-lc.go; see the GM section for a similar entry. Local job-manager registration:
cmd/sedna-lc/app/server.go
// runServer runs server
func runServer() {
c := gmclient.NewWebSocketClient(Options)
if err := c.Start(); err != nil {
return
}
dm := dataset.New(c, Options)
mm := model.New(c)
jm := jointinference.New(c)
fm := federatedlearning.New(c)
im := incrementallearning.New(c, dm, mm, Options)
lm := lifelonglearning.New(c, dm, Options)
s := server.New(Options)
for _, m := range []managers.FeatureManager{
dm, mm, jm, fm, im, lm,
} {
s.AddFeatureManager(m)
c.Subscribe(m)
err := m.Start()
if err != nil {
klog.Errorf("failed to start manager %s: %v",
m.GetName(), err)
return
}
klog.Infof("manager %s is started", m.GetName())
}
s.ListenAndServe()
}Local job management
Manager does edge job management. Shape:
pkg/localcontroller/managers/lifelonglearning/lifelonglearningjob.go
// LifelongLearningJobManager defines lifelong-learning-job Manager
type Manager struct {
Client clienttypes.ClientI
WorkerMessageChannel chan workertypes.MessageContent
DatasetManager *dataset.Manager
LifelongLearningJobMap map[string]*Job
VolumeMountPrefix string
}startJob():
- Watch Dataset objects synced to the edge; e.g. sample count hits a threshold → start train.
- By current stage, trigger train / eval / deploy. The edge does not start those jobs itself; it reports status to GM, GM schedules them.
pkg/localcontroller/managers/lifelonglearning/lifelonglearningjob.go
// startJob starts a job
func (lm *Manager) startJob(name string) {
...
// 监控并处理同步到边缘的Dataset对象。
go lm.handleData(job)
tick := time.NewTicker(JobIterationIntervalSeconds * time.Second)
for {
// 根据当前任务不同阶段,触发不同阶段的训练、评估、部署任务。
select {
case <-job.JobConfig.Done:
return
case <-tick.C:
cond := lm.getLatestCondition(job)
jobStage := cond.Stage
switch jobStage {
case sednav1.LLJobTrain:
err = lm.trainTask(job)
case sednav1.LLJobEval:
err = lm.evalTask(job)
case sednav1.LLJobDeploy:
err = lm.deployTask(job)
default:
klog.Errorf("invalid phase: %s", jobStage)
continue
}
...
}
}
}Besides the flow: dataset watch, model download, local DB backup of jobs, etc.
Message proxy
Besides pushing status to the cloud, LC starts an HTTP server on 0.0.0.0:9100 that takes messages from the Lib, folds them, and sends them to GM. Routes and handler:
pkg/localcontroller/server/server.go
// register registers api
func (s *Server) register(container *restful.Container) {
ws := new(restful.WebService)
ws.Path(fmt.Sprintf("/%s", constants.ServerRootPath)).
Consumes(restful.MIME_XML, restful.MIME_JSON).
Produces(restful.MIME_JSON, restful.MIME_XML)
ws.Route(ws.POST("/workers/{worker-name}/info").
To(s.messageHandler).
Doc("receive worker message"))
container.Add(ws)
}pkg/localcontroller/server/server.go
// messageHandler handles message from the worker
func (s *Server) messageHandler(request *restful.Request, response *restful.Response) {
var err error
workerName := request.PathParameter("worker-name")
workerMessage := workertypes.MessageContent{}
err = request.ReadEntity(&workerMessage)
if workerMessage.Name != workerName || err != nil {
var msg string
if workerMessage.Name != workerName {
msg = fmt.Sprintf("worker name(name=%s) in the api is different from that(name=%s) in the message body",
workerName, workerMessage.Name)
} else {
msg = fmt.Sprintf("read worker(name=%s) message body failed, error: %v", workerName, err)
}
klog.Errorf(msg)
err = s.reply(response, http.StatusBadRequest, msg)
if err != nil {
klog.Errorf("reply messge to worker(name=%s) failed, error: %v", workerName, err)
}
}
if m, ok := s.fmm[workerMessage.OwnerKind]; ok {
m.AddWorkerMessage(workerMessage)
}
err = s.reply(response, http.StatusOK, "OK")
if err != nil {
klog.Errorf("reply message to worker(name=%s) failed, error: %v", workerName, err)
return
}
}Sedna Lib source (Python)
Lib is a Python library for AI / app developers, so you can turn existing code into cloud–edge collab.
Layout:
➜ sedna tree lib -L 2
lib
├── __init__.py
├── MANIFEST.in
├── OWNERS
├── requirements.dev.txt
├── requirements.txt // Sedna Python的依赖
├── sedna
│ ├── algorithms // 边云协同算法
│ ├── backend // 支持的后端,tensorflow/pytorch
│ ├── common
│ ├── core // 主要特性的实现逻辑
│ ├── datasources // 支持的数据源格式,比如txt、csv等
│ ├── __init__.py
│ ├── README.md
│ ├── service // 需要启动server的组件,比如kb等
│ ├── VERSION
│ └── __version__.py
└── setup.pyTypical bits from each part:
core
core wraps user callbacks. train below mainly calls the user’s tensorflow / pytorch / mindspore train.
- Configure post-process.
- Call the cloud knowledge base for train / infer.
- Update the cloud KB. In lifelong learning the KB holds new models and samples and keeps changing.
- Report the train job to LC — done or not, metrics after train.
lib/sedna/core/lifelong_learning/lifelong_learning.py
def train(self, train_data,
valid_data=None,
post_process=None,
**kwargs):
is_completed_initilization = \
str(Context.get_parameters("HAS_COMPLETED_INITIAL_TRAINING",
"false")).lower()
if is_completed_initilization == "true":
return self.update(train_data,
valid_data=valid_data,
post_process=post_process,
**kwargs)
# 配置后处理函数
callback_func = None
if post_process is not None:
callback_func = ClassFactory.get_cls(
ClassType.CALLBACK, post_process)
res, seen_task_index = \
self.cloud_knowledge_management.seen_estimator.train(
train_data=train_data,
valid_data=valid_data,
**kwargs
)
# 调用云端知识库进行训练、或推理
unseen_res, unseen_task_index = \
self.cloud_knowledge_management.unseen_estimator.train()
# 更新云端知识库
task_index = dict(
seen_task=seen_task_index,
unseen_task=unseen_task_index)
task_index_url = FileOps.dump(
task_index, self.cloud_knowledge_management.local_task_index_url)
task_index = self.cloud_knowledge_management.update_kb(task_index_url)
res.update(unseen_res)
...
# 将当前训练任务执行的情况发送给LC,比如训练任务是否完成、训练后的指标是多少
self.report_task_info(
None, K8sResourceKindStatus.COMPLETED.value, task_info_res)
self.log.info(f"Lifelong learning Train task Finished, "
f"KB index save in {task_index}")
return callback_func(self.estimator, res) if callback_func else res
...backend
MSBackend is a Sedna-supported backend, MindSpore. If a framework has typical train / predict / evaluate, Sedna Lib can take it as a backend and wrap existing AI code for cloud–edge.
lib/sedna/backend/mindspore/__init__.py
class MSBackend(BackendBase):
def __init__(self, estimator, fine_tune=True, **kwargs):
super(MSBackend, self).__init__(estimator=estimator,
fine_tune=fine_tune,
**kwargs)
self.framework = "mindspore"
if self.use_npu:
context.set_context(mode=context.GRAPH_MODE,
device_target="Ascend")
elif self.use_cuda:
context.set_context(mode=context.GRAPH_MODE,
device_target="GPU")
else:
context.set_context(mode=context.GRAPH_MODE,
device_target="CPU")
if callable(self.estimator):
self.estimator = self.estimator()
def train(self, train_data, valid_data=None, **kwargs):
if callable(self.estimator):
self.estimator = self.estimator()
if self.fine_tune and FileOps.exists(self.model_save_path):
self.finetune()
self.has_load = True
varkw = self.parse_kwargs(self.estimator.train, **kwargs)
return self.estimator.train(train_data=train_data,
valid_data=valid_data,
**varkw)
def predict(self, data, **kwargs):
if not self.has_load:
self.load()
varkw = self.parse_kwargs(self.estimator.predict, **kwargs)
return self.estimator.predict(data=data, **varkw)
def evaluate(self, data, **kwargs):
if not self.has_load:
self.load()
varkw = self.parse_kwargs(self.estimator.evaluate, **kwargs)
return self.estimator.evaluate(data, **varkw)datasource
datasource wraps common dataset-format parsers, so you don’t have to
lib/sedna/datasources/__init__.py
class CSVDataParse(BaseDataSource, ABC):
"""
csv file which contain Structured Data parser
"""
# 提供了方便的数据集解析函数,
def parse(self, *args, **kwargs):
x_data = []
y_data = []
label = kwargs.pop("label") if "label" in kwargs else ""
usecols = kwargs.get("usecols", "")
if usecols and isinstance(usecols, str):
usecols = usecols.split(",")
if len(usecols):
if label and label not in usecols:
usecols.append(label)
kwargs["usecols"] = usecols
for f in args:
if isinstance(f, (dict, list)):
res = self.parse_json(f, **kwargs)
else:
if not (f and FileOps.exists(f)):
continue
res = pd.read_csv(f, **kwargs)
if self.process_func and callable(self.process_func):
res = self.process_func(res)
if label:
if label not in res.columns:
continue
y = res[label]
y_data.append(y)
res.drop(label, axis=1, inplace=True)
x_data.append(res)
if not x_data:
return
self.x = pd.concat(x_data)
self.y = pd.concat(y_data)algorithms
Cloud–edge AI needs algorithms for that setting. Sedna ships a few hard-example miners, e.g. the cross-entropy threshold below, which can flag samples when the edge model is not confident.
The point isn’t only to ship those basics — it’s to let the cloud–edge framework grow more practical algorithms that improve train/infer overall. That’s what this Lib shape is for.
lib/sedna/algorithms/hard_example_mining/hard_example_mining.py
@ClassFactory.register(ClassType.HEM, alias="CrossEntropy")
class CrossEntropyFilter(BaseFilter, abc.ABC):
"""
**Object detection** Hard samples discovery methods named `CrossEntropy`
Parameters
----------
threshold_cross_entropy: float
hard coefficient threshold score to filter img, default to 0.5.
"""
def __init__(self, threshold_cross_entropy=0.5, **kwargs):
self.threshold_cross_entropy = float(threshold_cross_entropy)
def __call__(self, infer_result=None) -> bool:
"""judge the img is hard sample or not.
Parameters
----------
infer_result: array_like
prediction classes list, such as
[class1-score, class2-score, class2-score,....],
where class-score is the score corresponding to the class,
class-score value is in [0,1], who will be ignored if its
value not in [0,1].
Returns
-------
is hard sample: bool
`True` means hard sample, `False` means not.
"""
if not infer_result:
# if invalid input, return False
return False
log_sum = 0.0
data_check_list = [class_probability for class_probability
in infer_result
if self.data_check(class_probability)]
if len(data_check_list) != len(infer_result):
return False
for class_data in data_check_list:
log_sum += class_data * math.log(class_data)
confidence_score = 1 + 1.0 * log_sum / math.log(
len(infer_result))
return confidence_score < self.threshold_cross_entropy