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
//! Narrow capability contracts implemented by execution backends.
use eredu_checkpoint::{
recipe::DerivedWeightRecipe,
store::{CheckpointLease, CheckpointSource},
};
use eredu_core::Completion;
use eredu_nn::NeuralBackend;
/// Submits backend-native work and retains values through exact completion.
pub trait SubmissionBackend: NeuralBackend {
/// Backend executor, queue, stream, or equivalent submission context.
type Executor: ?Sized;
/// Owned executor used for an independently schedulable graph lane.
type OwnedExecutor: std::borrow::Borrow<Self::Executor>;
/// Exact completion object for one submission.
type Completion: Completion;
/// Creates independently schedulable executors on the same backend device.
fn fork_executors(
executor: &Self::Executor,
count: usize,
) -> Result<Vec<Self::OwnedExecutor>, <Self::Completion as Completion>::Error>;
/// Submits evaluation of backend-native values on one executor.
fn submit<'a, I>(
executor: &Self::Executor,
values: I,
) -> Result<Self::Completion, <Self::Completion as Completion>::Error>
where
Self::Tensor: 'a,
I: IntoIterator<Item = &'a Self::Tensor>;
/// Orders future work on `executor` after an exact producer completion.
fn order_after(
completion: &Self::Completion,
executor: &Self::Executor,
) -> Result<(), <Self::Completion as Completion>::Error>;
/// Retains an owned value until `completion` has completed exactly.
fn retain_until_complete<T: Send + 'static>(
executor: &Self::Executor,
completion: &Self::Completion,
value: T,
) -> Result<(), <Self::Completion as Completion>::Error>;
}
/// Materializes and binds checkpoint data to backend-native parameter slots.
pub trait ParameterBackend: NeuralBackend {
/// One backend-native parameter slot.
type Parameter: 'static;
/// Materialized backend-native checkpoint weight.
type MaterializedWeight;
/// Backend context used only while realizing checkpoint parameters.
type MaterializationContext: ?Sized;
/// In-flight guard retaining encoded sources through exact realization completion.
type Materialization;
/// Backend-specific loading failure.
type ParameterError: std::error::Error + Send + Sync + 'static;
/// Lowers one format-preserving encoded lease into a native weight.
fn materialize(
lease: CheckpointLease,
context: &Self::MaterializationContext,
) -> Result<Self::Materialization, Self::ParameterError>;
/// Lowers a validated neutral recipe directly into a native weight.
fn materialize_recipe(
recipe: &DerivedWeightRecipe,
source: &dyn CheckpointSource,
context: &Self::MaterializationContext,
) -> Result<Self::Materialization, Self::ParameterError>;
/// Borrows the native weight retained by an in-flight materialization.
fn materialized_weight(materialization: &Self::Materialization) -> &Self::MaterializedWeight;
/// Waits for this exact realization and releases its encoded source lease.
fn finish_materialization(
materialization: Self::Materialization,
) -> Result<Self::MaterializedWeight, Self::ParameterError>;
/// Creates another native handle to identical materialized storage without
/// rereading or rematerializing checkpoint data.
fn share_materialized_weight(
weight: &Self::MaterializedWeight,
) -> Result<Self::MaterializedWeight, Self::ParameterError>;
/// Validates destination shape/storage compatibility without publication.
fn validate_bind(
parameter: &Self::Parameter,
weight: &Self::MaterializedWeight,
) -> Result<(), Self::ParameterError>;
/// Binds one materialized weight to its destination parameter.
///
/// After successful [`Self::validate_bind`] on unchanged arguments this
/// operation must not fail, allowing orchestration to validate an entire
/// atomic unit before publishing any destination.
fn bind(
parameter: &mut Self::Parameter,
weight: Self::MaterializedWeight,
) -> Result<(), Self::ParameterError>;
}
/// Promotes and demotes backend-native storage without changing its semantics.
pub trait TransferBackend: SubmissionBackend + ParameterBackend {
/// Backend-owned host representation.
type HostBuffer;
/// In-flight transfer guard retaining all source and destination storage.
type Transfer: Completion<Error = Self::TransferError>;
/// Backend-specific transfer failure.
type TransferError: std::error::Error + Send + Sync + 'static;
/// Promotes host storage into a materialized execution weight.
fn promote(
executor: &Self::Executor,
host: &Self::HostBuffer,
) -> Result<(Self::MaterializedWeight, Self::Transfer), Self::TransferError>;
/// Demotes a materialized execution weight into backend-owned host storage.
fn demote(
executor: &Self::Executor,
weight: &Self::MaterializedWeight,
) -> Result<(Self::HostBuffer, Self::Transfer), Self::TransferError>;
}
/// Collective operations available to distributed runtime policies.
pub trait CollectiveBackend: SubmissionBackend {
/// Backend-native collective group.
type Group: ?Sized;
/// Backend-specific collective failure.
type CollectiveError: std::error::Error + Send + Sync + 'static;
/// Reduces a tensor across the selected group.
fn all_reduce(
value: Self::Tensor,
group: &Self::Group,
executor: &Self::Executor,
) -> Result<Self::Tensor, Self::CollectiveError>;
/// Gathers a tensor across the selected group.
fn all_gather(
value: Self::Tensor,
group: &Self::Group,
executor: &Self::Executor,
) -> Result<Self::Tensor, Self::CollectiveError>;
/// Exchanges tensor partitions across the selected group.
fn all_to_all(
value: Self::Tensor,
group: &Self::Group,
executor: &Self::Executor,
) -> Result<Self::Tensor, Self::CollectiveError>;
}