now I want to do a multiply-transpose for a dense layer - I have the forward version done.
could you rewrite normal version (for non transposed multiplications), into the transpose version (for dense):
code
ML_GLOBAL_FN
void ml_multiply_backward_via_accelerate(
ML_RUNTIME_PARAMS
Size instances,
const Ptr<Real> a_, const Per::Value a_per, const Dim2 a_dim,
const Ptr<Real> b_, const Per::Value b_per, const Dim2 b_dim,
const Ptr<Real> dout_,
Ptr<Real> da_, Ptr<Real> db_,
Ptr<Real> dbias_, const Dim2 bias_dim
)
{
const auto runtime = get_runtime(ML_RUNTIME);
const auto instance = ml_index(runtime);
if (instance >= instances)
return;
const Dim2 o_dim(b_dim[0], a_dim[1]);
auto a_size = a_dim.size();
auto b_size = b_dim.size();
auto out_size = o_dim.size();
auto bias_size = bias_dim.size();
auto a = forward_instance(a_per, a_, instance, a_size);
auto b = forward_instance(b_per, b_, instance, b_size);
auto da = backward_instance(da_, instance, a_size);
auto db = backward_instance(db_, instance, b_size);
auto dbias = backward_instance(dbias_, instance, bias_size);
auto dout = backward_instance(dout_, instance, out_size);
const Real alpha = 1;
const Real beta = 0;
// const Dim2 o_dim(b_dim[0], a_dim[1]);
// a[a0,a1] * b[b0,b1=a0] = o[b0,a1]
// o[b0,a1] * (b[b0,b1=a0]T->b[b1=a0,b0]) = a[a0,a1]
cblas_sgemm(
CblasRowMajor,
CblasNoTrans,
CblasTrans,
// outer
a_dim[1], a_dim[0],
// inner
b_dim[0],
alpha,
unsafe_ptr(dout), o_dim[0],
unsafe_ptr(b), b_dim[0],
beta,
unsafe_ptr(da), a_dim[0]
);
// const Dim2 o_dim(b_dim[0], a_dim[1]);
// a[a0=b1,a1] * b[b0,b1=a0] = o[b0,a1]
// o[b0,a1]T=>o[a1,b0] * a[a0=b1,a1] = b[a0=b1,b0]
// (a[a0=b1,a1]T=>a[a1,a0=b1]) * o[b0,a1] = b[b0, a0=b1]
cblas_sgemm(
CblasRowMajor,
CblasTrans,
CblasNoTrans,
// outer MN
b_dim[1], b_dim[0],
// inner K
a_dim[1],
alpha,
unsafe_ptr(a), a_dim[0], // ldb tied to M = b_dim[1] = a_dim[0]
unsafe_ptr(dout), b_dim[0], // lda tied to N = b_dim[0]
beta,
unsafe_ptr(db), b_dim[0] // ldc tied to N
);
ml_for_loop_2(bias_i, bias_dim)
dbias[bias_dim(bias_i0, bias_i1)] = 0;
auto b_i0M = bias_dim[0] - 1;
auto b_i1M = bias_dim[1] - 1;
ml_for_loop_2(o_i, o_dim)
{
auto bias_i0 = ml_min(b_i0M, o_i0);
auto bias_i1 = ml_min(b_i1M, o_i1);
dbias[bias_dim(bias_i0, bias_i1)] += dout[o_dim(o_i0, o_i1)];
}
}
Assistant
c
ML_GLOBAL_FN
void ml_multiply_transpose_backward_via_accelerate(
ML_RUNTIME_PARAMS
Size instances,
const Ptr<Real> a_, const Per::Value a_per, const Dim2 a_dim, // A (typically input features for dense layer: M x K)
const Ptr<Real> b_, const Per::Value b_per, const Dim2 b_dim, // B (typically weights for dense layer, NOT transposed: N x K)
const Ptr<Real> dout_, // dO (gradient of output: M x N)
Ptr<Real> da_, // dA (gradient w.r.t. A: M x K)
Ptr<Real> db_, // dB (gradient w.r.t. B: N x K)
Ptr<Real> dbias_, const Dim2 bias_dim // dBias
)
{
const auto runtime = get_runtime(ML_RUNTIME);
const auto instance = ml_index(runtime);
if (instance >= instances)
return;
// This function implements the backward pass for the forward operation:
// O = A @ B^T
// Where:
// A is a_dim[0] rows by a_dim[1] columns (e.g., input (BatchSize x InputFeatures))
// B is b_dim[0] rows by b_dim[1] columns (e.g., weights (OutputFeatures x InputFeatures))
// B^T is b_dim[1] rows by b_dim[0] columns
//
// For the multiplication A @ B^T to be valid:
// a_dim[1] (cols of A) must be equal to b_dim[1] (rows of B^T, which is cols of B)
// So, the inner dimension K = a_dim[1] = b_dim[1]
//
// The output O will have dimensions:
// o_dim[0] = a_dim[0] (rows of A)
// o_dim[1] = b_dim[0] (cols of B^T, which is rows of B)
//
// Note on dimensions and cblas_sgemm ldx:
// Assuming Dim2(rows, cols) and CblasRowMajor.
// Historically, the provided 'normal' multiply backward code used dim[0] (rows) for ldx (leading dimension).
// This is non-standard for CblasRowMajor (which expects cols for ldx), but if it works for the user,
// we will follow this convention for consistency in the rewritten code.
const Dim2 o_dim(a_dim[0], b_dim[0]); // Corrected o_dim for O = A @ B^T
auto a_size = a_dim.size();
auto b_size = b_dim.size();
auto out_size = o_dim.size();
auto bias_size = bias_dim.size();
auto a = forward_instance(a_per, a_, instance, a_size);
auto b = forward_instance(b_per, b_, instance, b_size); // B is weights (N x K)
auto da = backward_instance(da_, instance, a_size); // dA is gradient for A (M x K)
auto db = backward_instance(db_, instance, b_size); // dB is gradient for B (N x K)
auto dbias = backward_instance(dbias_, instance, bias_size);
auto dout = backward_instance(dout_, instance, out_size); // dO is gradient for O (M x N)
const Real alpha = 1;
const Real beta = 0;
// Backward pass for O = A @ B^T:
// dA = dO @ B
// dB = dO^T @ A
// 1. Calculate dA = dO @ B
// Dimensions:
// dA (output matrix): a_dim[0] rows x a_dim[1] cols (M x K)
// dO (input matrix A for sgemm): o_dim[0] rows x o_dim[1] cols = a_dim[0] x b_dim[0] (M x N)
// B (input matrix B for sgemm): b_dim[0] rows x b_dim[1] cols (N x K)
// For the sgemm call: (M x N) @ (N x K) = (M x K)
// m = rows of dO / dA = a_dim[0]
// n = cols of B / dA = b_dim[1]
// k = inner dim (cols of dO / rows of B) = b_dim[0]
cblas_sgemm(
CblasRowMajor,
CblasNoTrans, // dO (dout) is not transposed
CblasNoTrans, // B (b) is not transposed
a_dim[0], // m: rows of the result dA (which is rows of dout/dO)
b_dim[1], // n: cols of the result dA (which is cols of b/B)
b_dim[0], // k: inner dimension (cols of dout/dO, rows of b/B)
alpha,
unsafe_ptr(dout), o_dim[0], // lda for dout: rows of dout (as per original code's ldx pattern)
unsafe_ptr(b), b_dim[0], // ldb for b: rows of b (as per original code's ldx pattern)
beta,
unsafe_ptr(da), a_dim[0] // ldc for da: rows of da (as per original code's ldx pattern)
);
// 2. Calculate dB = dO^T @ A
// Dimensions:
// dB (output matrix): b_dim[0] rows x b_dim[1] cols (N x K)
// dO^T (input matrix A for sgemm): o_dim[1] rows x o_dim[0] cols = b_dim[0] x a_dim[0] (N x M)
// A (input matrix B for sgemm): a_dim[0] rows x a_dim[1] cols (M x K)
// For the sgemm call: (N x M) @ (M x K) = (N x K)
// m = rows of dO^T / dB = b_dim[0]
// n = cols of A / dB = a_dim[1]
// k = inner dim (cols of dO^T / rows of A) = a_dim[0]
cblas_sgemm(
CblasRowMajor,
CblasTrans, // dO (dout) is transposed
CblasNoTrans, // A (a) is not transposed
b_dim[0], // m: rows of the result dB (which is rows of dout/dO transposed)
a_dim[1], // n: cols of the result dB (which is cols of a/A)
a_dim[0], // k: inner dimension (cols of dout/dO transposed, rows of a/A)
alpha,
unsafe_ptr(dout), o_dim[0], // lda for dout: rows of dout (as per original code's ldx pattern)
unsafe_ptr(a), a_dim[0], // ldb for a: rows of a (as per original code's ldx pattern)
beta,
unsafe_ptr(db), b_dim[0] // ldc for db: rows of db (as per original code's ldx pattern)
);
// Bias calculation remains the same, as it's typically a sum over the batch dimension.
// Assuming bias_dim represents (1, OutputFeatures) and o_dim[1] is OutputFeatures.
ml_for_loop_2(bias_i, bias_dim)
dbias[bias_dim(bias_i0, bias_i1)] = 0;
auto b_i0M = bias_dim[0] - 1;
auto b_i1M = bias_dim[1] - 1;
ml_for_loop_2(o_i, o_dim)
{
auto bias_i0 = ml_min(b_i0M, o_i0);
auto bias_i1 = ml_min(b_i1M, o_i1);
dbias[bias_dim(bias_i0, bias_i1)] += dout[o_dim(o_i0, o_i1)];
}
}