"The edge is not one place. It is every place where data is born before the cloud ever sees it."

# Train your model normally in SageMaker, then compile it
# for the target edge hardware (e.g., NVIDIA Jetson)
import boto3
sagemaker_client = boto3.client('sagemaker')
compilation_job = sagemaker_client.create_compilation_job(
CompilationJobName='my-model-jetson-agx',
RoleArn='arn:aws:iam::123456789012:role/SageMakerRole',
InputConfig={
'S3Uri': 's3://my-bucket/models/defect-detector.tar.gz',
'DataInputConfig': '{"input": [1, 3, 224, 224]}', # image shape
'Framework': 'PYTORCH'
},
OutputConfig={
'S3OutputLocation': 's3://my-bucket/compiled/',
'TargetPlatform': {
'Os': 'LINUX',
'Arch': 'ARM64',
'Accelerator': 'NVIDIA' # targets the Jetson GPU
}
},
StoppingCondition={'MaxRuntimeInSeconds': 900}
)
print(f"Compilation job started: {compilation_job['CompilationJobArn']}")# On the Greengrass core device, use the SageMaker Edge Agent
# to load and run the compiled model
import edge_agent_pb2 as pb2
import edge_agent_pb2_grpc as pb2_grpc
import grpc
import numpy as np
# Connect to the local SageMaker Edge Agent (runs as a daemon)
channel = grpc.insecure_channel('unix:///tmp/sagemaker_edge_agent_example.sock')
stub = pb2_grpc.AgentStub(channel)
# Load model (one-time setup)
stub.LoadModel(pb2.LoadModelRequest(
url='/greengrass/v2/models/defect-detector',
name='defect-detector'
))
# Run inference on an image captured from local camera
def run_inference(image_array):
tensor = pb2.Tensor(
tensor_metadata=pb2.TensorMetadata(
name='input',
data_type=5, # FLOAT32
shape=[1, 3, 224, 224]
),
byte_data=image_array.tobytes()
)
response = stub.Predict(pb2.PredictRequest(
name='defect-detector',
tensors=[tensor]
))
return np.frombuffer(response.tensors[0].byte_data, dtype=np.float32)