-
Notifications
You must be signed in to change notification settings - Fork 880
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CSR implementation of
mj_factorI
PiperOrigin-RevId: 712498431 Change-Id: I13b52e53482ed97da8788875d4d95e2beb5ca7c1
- Loading branch information
1 parent
7eb8231
commit ac11e5f
Showing
9 changed files
with
514 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright 2025 DeepMind Technologies Limited | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// A benchmark for comparing different implementations of mj_factorI. | ||
|
||
#include <benchmark/benchmark.h> | ||
#include <absl/base/attributes.h> | ||
#include <mujoco/mjdata.h> | ||
#include <mujoco/mujoco.h> | ||
#include "src/engine/engine_core_smooth.h" | ||
#include "test/fixture.h" | ||
|
||
namespace mujoco { | ||
namespace { | ||
|
||
// number of steps to benchmark | ||
static const int kNumBenchmarkSteps = 50; | ||
|
||
// ----------------------------- benchmark ------------------------------------ | ||
|
||
static void BM_factorI(benchmark::State& state, bool legacy, bool coil) { | ||
static mjModel* m; | ||
if (coil) { | ||
m = LoadModelFromPath("plugin/elasticity/coil.xml"); | ||
} else { | ||
m = LoadModelFromPath("humanoid/humanoid100.xml"); | ||
} | ||
|
||
mjData* d = mj_makeData(m); | ||
mj_forward(m, d); | ||
|
||
// allocate inputs and outputs | ||
mj_markStack(d); | ||
|
||
// CSR matrices | ||
mjtNum* Ms = mj_stackAllocNum(d, m->nC); | ||
mjtNum* LDs = mj_stackAllocNum(d, m->nC); | ||
for (int i=0; i < m->nC; i++) { | ||
Ms[i] = d->qM[d->mapM2C[i]]; | ||
} | ||
|
||
// benchmark | ||
while (state.KeepRunningBatch(kNumBenchmarkSteps)) { | ||
for (int i=0; i < kNumBenchmarkSteps; i++) { | ||
if (legacy) { | ||
mj_factorI(m, d, d->qM, d->qLD, d->qLDiagInv); | ||
} else { | ||
mju_copy(LDs, Ms, m->nC); | ||
mj_factorIs(LDs, d->qLDiagInv, m->nv, | ||
d->C_rownnz, d->C_rowadr, m->dof_simplenum, d->C_colind); | ||
} | ||
} | ||
} | ||
|
||
// finalize | ||
mj_freeStack(d); | ||
mj_deleteData(d); | ||
mj_deleteModel(m); | ||
state.SetItemsProcessed(state.iterations()); | ||
} | ||
|
||
void ABSL_ATTRIBUTE_NO_TAIL_CALL | ||
BM_factorI_COIL_LEGACY(benchmark::State& state) { | ||
MujocoErrorTestGuard guard; | ||
BM_factorI(state, /*legacy=*/true, /*coil=*/true); | ||
} | ||
BENCHMARK(BM_factorI_COIL_LEGACY); | ||
|
||
void ABSL_ATTRIBUTE_NO_TAIL_CALL | ||
BM_factorI_COIL_CSR(benchmark::State& state) { | ||
MujocoErrorTestGuard guard; | ||
BM_factorI(state, /*legacy=*/false, /*coil=*/true); | ||
} | ||
BENCHMARK(BM_factorI_COIL_CSR); | ||
|
||
void ABSL_ATTRIBUTE_NO_TAIL_CALL | ||
BM_factorI_H100_LEGACY(benchmark::State& state) { | ||
MujocoErrorTestGuard guard; | ||
BM_factorI(state, /*legacy=*/true, /*coil=*/false); | ||
} | ||
BENCHMARK(BM_factorI_H100_LEGACY); | ||
|
||
void ABSL_ATTRIBUTE_NO_TAIL_CALL | ||
BM_factorI_H100_CSR(benchmark::State& state) { | ||
MujocoErrorTestGuard guard; | ||
BM_factorI(state, /*legacy=*/false, /*coil=*/false); | ||
} | ||
BENCHMARK(BM_factorI_H100_CSR); | ||
|
||
} // namespace | ||
} // namespace mujoco |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2025 DeepMind Technologies Limited | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// A benchmark for comparing legacy and two CSR implementations of inertia | ||
// factor and then solve. | ||
|
||
#include <benchmark/benchmark.h> | ||
#include <absl/base/attributes.h> | ||
#include <mujoco/mjdata.h> | ||
#include <mujoco/mujoco.h> | ||
#include "src/engine/engine_core_smooth.h" | ||
#include "test/fixture.h" | ||
|
||
namespace mujoco { | ||
namespace { | ||
|
||
// number of steps to benchmark | ||
static const int kNumBenchmarkSteps = 50; | ||
|
||
// ----------------------------- benchmark ------------------------------------ | ||
|
||
enum class SolveType { | ||
kLegacy = 0, | ||
kCsr, | ||
}; | ||
|
||
static void BM_solve(benchmark::State& state, SolveType type) { | ||
static mjModel* m; | ||
m = LoadModelFromPath("../test/benchmark/testdata/inertia.xml"); | ||
|
||
mjData* d = mj_makeData(m); | ||
mj_forward(m, d); | ||
|
||
// allocate input and output vectors | ||
mj_markStack(d); | ||
|
||
// make CSR matrix | ||
mjtNum* Ms = mj_stackAllocNum(d, m->nC); | ||
mjtNum* LDs = mj_stackAllocNum(d, m->nC); | ||
for (int i=0; i < m->nC; i++) { | ||
Ms[i] = d->qM[d->mapM2C[i]]; | ||
} | ||
|
||
// arbitrary input vector | ||
mjtNum *res = mj_stackAllocNum(d, m->nv); | ||
mjtNum *vec = mj_stackAllocNum(d, m->nv); | ||
for (int i=0; i < m->nv; i++) { | ||
vec[i] = 0.2 + 0.3*i; | ||
} | ||
|
||
// benchmark | ||
while (state.KeepRunningBatch(kNumBenchmarkSteps)) { | ||
for (int i=0; i < kNumBenchmarkSteps; i++) { | ||
switch (type) { | ||
case SolveType::kLegacy: | ||
mj_factorI(m, d, d->qM, d->qLD, d->qLDiagInv); | ||
mj_solveM(m, d, res, vec, 1); | ||
break; | ||
case SolveType::kCsr: | ||
mju_copy(LDs, Ms, m->nC); | ||
mj_factorIs(LDs, d->qLDiagInv, m->nv, | ||
d->C_rownnz, d->C_rowadr, m->dof_simplenum, d->C_colind); | ||
mju_copy(res, vec, m->nv); | ||
mj_solveLDs(res, LDs, d->qLDiagInv, m->nv, | ||
d->C_rownnz, d->C_rowadr, m->dof_simplenum, d->C_colind); | ||
} | ||
} | ||
} | ||
|
||
// finalize | ||
mj_freeStack(d); | ||
mj_deleteData(d); | ||
mj_deleteModel(m); | ||
state.SetItemsProcessed(state.iterations()); | ||
} | ||
|
||
void ABSL_ATTRIBUTE_NO_TAIL_CALL BM_solve_LEGACY(benchmark::State& state) { | ||
MujocoErrorTestGuard guard; | ||
BM_solve(state, SolveType::kLegacy); | ||
} | ||
BENCHMARK(BM_solve_LEGACY); | ||
|
||
void ABSL_ATTRIBUTE_NO_TAIL_CALL BM_solve_CSR(benchmark::State& state) { | ||
MujocoErrorTestGuard guard; | ||
BM_solve(state, SolveType::kCsr); | ||
} | ||
BENCHMARK(BM_solve_CSR); | ||
|
||
} // namespace | ||
} // namespace mujoco |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.