From 1bf2191987186c79ce6556dd434c5b1938de0cd7 Mon Sep 17 00:00:00 2001 From: klowrey Date: Thu, 16 Sep 2021 10:53:30 -0700 Subject: [PATCH] removed unsafe_arrays and uviews --- Project.toml | 2 - src/LyceumMuJoCo.jl | 1 - src/dmc/cartpole_swingup.jl | 29 ++++-------- src/gym/hopper-v2.jl | 76 ++++++++++++------------------ src/gym/swimmer-v2.jl | 38 +++++---------- src/gym/walker2d-v2.jl | 66 +++++++++++--------------- src/mjsim.jl | 12 ++--- src/suite/armhand/armhandpickup.jl | 2 +- src/suite/pointmass.jl | 14 ++---- 9 files changed, 91 insertions(+), 149 deletions(-) diff --git a/Project.toml b/Project.toml index 29e2ec9..e1efd98 100644 --- a/Project.toml +++ b/Project.toml @@ -15,7 +15,6 @@ Reexport = "189a3867-3050-52da-a836-e630ba90ab69" Shapes = "175de200-b73b-11e9-28b7-9b5b306cec37" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" -UnsafeArrays = "c4a57d5a-5b31-53a6-b365-19f8c011fbd6" [compat] Distances = "0.8" @@ -26,7 +25,6 @@ MuJoCo = "0.3" Reexport = "0.2" Shapes = "0.2" StaticArrays = "0.12" -UnsafeArrays = "1" julia = "1.3" [extras] diff --git a/src/LyceumMuJoCo.jl b/src/LyceumMuJoCo.jl index d1cc78f..39bc3cf 100644 --- a/src/LyceumMuJoCo.jl +++ b/src/LyceumMuJoCo.jl @@ -8,7 +8,6 @@ using LinearAlgebra using Random # 3rd party -using UnsafeArrays using StaticArrays using Distributions using Reexport diff --git a/src/dmc/cartpole_swingup.jl b/src/dmc/cartpole_swingup.jl index 5e7f7ed..2d0fe2b 100644 --- a/src/dmc/cartpole_swingup.jl +++ b/src/dmc/cartpole_swingup.jl @@ -56,24 +56,19 @@ end @inline getsim(env::CartpoleSwingup) = env.sim - - @inline obsspace(env::CartpoleSwingup) = env.obsspace @inline function getobs!(obs, env::CartpoleSwingup) @boundscheck checkaxes(obsspace(env), obs) - @uviews obs begin - sobs = obsspace(env)(obs) - sobs.pos.cart = env.sim.dn.qpos[:slider] - sobs.pos.pole_zz = env.sim.dn.xmat[:z, :z, :pole_1] - sobs.pos.pole_xz = env.sim.dn.xmat[:x, :z, :pole_1] - copyto!(sobs.vel, env.sim.d.qvel) - end + sobs = obsspace(env)(obs) + sobs.pos.cart = env.sim.dn.qpos[:slider] + sobs.pos.pole_zz = env.sim.dn.xmat[:z, :z, :pole_1] + sobs.pos.pole_xz = env.sim.dn.xmat[:x, :z, :pole_1] + copyto!(sobs.vel, env.sim.d.qvel) obs end - @inline function getreward(state, action, obs, env::CartpoleSwingup) @boundscheck begin checkaxes(statespace(env), state) @@ -97,7 +92,6 @@ end mean(upright) * small_control * small_velocity * centered end - @inline function geteval(state, action, obs, env::CartpoleSwingup) @boundscheck begin checkaxes(obsspace(env), obs) @@ -105,14 +99,11 @@ end obsspace(env)(obs).pos.pole_zz end - function reset!(env::CartpoleSwingup) reset_nofwd!(env.sim) qpos = env.sim.dn.qpos - @uviews qpos begin - qpos[:hinge_1] = pi - end + qpos[:hinge_1] = pi forward!(env.sim) @@ -123,10 +114,8 @@ function randreset!(rng::Random.AbstractRNG, env::CartpoleSwingup) reset_nofwd!(env.sim) qpos = env.sim.dn.qpos - @uviews qpos begin - qpos[:slider] = 0.01 * randn(rng) - qpos[:hinge_1] = pi + 0.01*randn(rng) - end + qpos[:slider] = 0.01 * randn(rng) + qpos[:hinge_1] = pi + 0.01*randn(rng) randn!(rng, env.sim.d.qvel) env.sim.d.qvel .*= 0.01 @@ -134,4 +123,4 @@ function randreset!(rng::Random.AbstractRNG, env::CartpoleSwingup) forward!(env.sim) env -end \ No newline at end of file +end diff --git a/src/gym/hopper-v2.jl b/src/gym/hopper-v2.jl index 2fe564a..8dcc116 100644 --- a/src/gym/hopper-v2.jl +++ b/src/gym/hopper-v2.jl @@ -41,37 +41,30 @@ function tconstruct(::Type{HopperV2}, n::Integer) end @inline getsim(env::HopperV2) = env.sim - - @inline statespace(env::HopperV2) = env.statespace function getstate!(state, env::HopperV2) checkaxes(statespace(env), state) - @uviews state begin - shaped = statespace(env)(state) - getstate!(shaped.simstate, env.sim) - shaped.last_torso_x = env.last_torso_x - end + shaped = statespace(env)(state) + getstate!(shaped.simstate, env.sim) + shaped.last_torso_x = env.last_torso_x state end function setstate!(env::HopperV2, state) checkaxes(statespace(env), state) - @uviews state begin - shaped = statespace(env)(state) - setstate!(env.sim, shaped.simstate) - env.last_torso_x = shaped.last_torso_x - end + shaped = statespace(env)(state) + setstate!(env.sim, shaped.simstate) + env.last_torso_x = shaped.last_torso_x env end - @inline obsspace(env::HopperV2) = env.obsspace function getobs!(obs, env::HopperV2) checkaxes(obsspace(env), obs) qpos = env.sim.d.qpos - @views @uviews qpos obs begin + @views begin shaped = obsspace(env)(obs) copyto!(shaped.cropped_qpos, qpos[2:end]) copyto!(shaped.qvel, env.sim.d.qvel) @@ -80,28 +73,22 @@ function getobs!(obs, env::HopperV2) obs end - function getreward(state, action, ::Any, env::HopperV2) checkaxes(statespace(env), state) checkaxes(actionspace(env), action) - @uviews state begin - shapedstate = statespace(env)(state) - alive_bonus = 1.0 - reward = (_torso_x(shapedstate, env) - shapedstate.last_torso_x) / timestep(env) - reward += alive_bonus - reward -= 1e-3 * sum(x->x^2, action) - reward - end + shapedstate = statespace(env)(state) + alive_bonus = 1.0 + reward = (_torso_x(shapedstate, env) - shapedstate.last_torso_x) / timestep(env) + reward += alive_bonus + reward -= 1e-3 * sum(x->x^2, action) + reward end function geteval(state, ::Any, ::Any, env::HopperV2) checkaxes(statespace(env), state) - @uviews state begin - _torso_x(statespace(env)(state), env) - end + _torso_x(statespace(env)(state), env) end - function reset!(env::HopperV2) reset!(env.sim) env.last_torso_x = _torso_x(env) @@ -117,7 +104,6 @@ function randreset!(rng::AbstractRNG, env::HopperV2) env end - function step!(env::HopperV2) env.last_torso_x = _torso_x(env) step!(env.sim) @@ -126,26 +112,24 @@ end function isdone(state, ::Any, ::Any, env::HopperV2) checkaxes(statespace(env), state) - @uviews state begin - shapedstate = statespace(env)(state) - torso_x = _torso_x(shapedstate, env) - height = _torso_height(shapedstate, env) - torso_ang = _torso_ang(shapedstate, env) - qpos = shapedstate.simstate.qpos - qvel = shapedstate.simstate.qvel - - done = !( - all(isfinite, state) - && all(x->abs(x) < 100, uview(qpos, 3:length(qpos))) - && all(x->abs(x) < 100, uview(qvel)) - && height > 0.7 - && abs(torso_ang) < 0.2 - ) - done - end + shapedstate = statespace(env)(state) + torso_x = _torso_x(shapedstate, env) + height = _torso_height(shapedstate, env) + torso_ang = _torso_ang(shapedstate, env) + qpos = shapedstate.simstate.qpos + qvel = shapedstate.simstate.qvel + + done = !( + all(isfinite, state) + && all(x->abs(x) < 100, view(qpos, 3:length(qpos))) + && all(x->abs(x) < 100, qvel) + && height > 0.7 + && abs(torso_ang) < 0.2 + ) + done end @inline _torso_x(shapedstate::ShapedView, ::HopperV2) = shapedstate.simstate.qpos[1] @inline _torso_x(env::HopperV2) = env.sim.d.qpos[1] @inline _torso_height(shapedstate::ShapedView, ::HopperV2) = shapedstate.simstate.qpos[2] -@inline _torso_ang(shapedstate::ShapedView, ::HopperV2) = shapedstate.simstate.qpos[3] \ No newline at end of file +@inline _torso_ang(shapedstate::ShapedView, ::HopperV2) = shapedstate.simstate.qpos[3] diff --git a/src/gym/swimmer-v2.jl b/src/gym/swimmer-v2.jl index f05f6a2..3b8b527 100644 --- a/src/gym/swimmer-v2.jl +++ b/src/gym/swimmer-v2.jl @@ -42,37 +42,30 @@ function tconstruct(::Type{SwimmerV2}, n::Integer) end @inline getsim(env::SwimmerV2) = env.sim - - @inline statespace(env::SwimmerV2) = env.statespace function getstate!(state, env::SwimmerV2) checkaxes(statespace(env), state) - @uviews state begin - shaped = statespace(env)(state) - getstate!(shaped.simstate, env.sim) - shaped.last_torso_x = env.last_torso_x - end + shaped = statespace(env)(state) + getstate!(shaped.simstate, env.sim) + shaped.last_torso_x = env.last_torso_x state end function setstate!(env::SwimmerV2, state) checkaxes(statespace(env), state) - @uviews state begin - shaped = statespace(env)(state) - setstate!(env.sim, shaped.simstate) - env.last_torso_x = shaped.last_torso_x - end + shaped = statespace(env)(state) + setstate!(env.sim, shaped.simstate) + env.last_torso_x = shaped.last_torso_x env end - @inline obsspace(env::SwimmerV2) = env.obsspace function getobs!(obs, env::SwimmerV2) checkaxes(obsspace(env), obs) qpos = env.sim.d.qpos - @views @uviews obs qpos begin + @views begin shaped = obsspace(env)(obs) copyto!(shaped.qpos_cropped, qpos[3:end]) copyto!(shaped.qvel, env.sim.d.qvel) @@ -80,23 +73,18 @@ function getobs!(obs, env::SwimmerV2) obs end - function getreward(state, action, ::Any, env::SwimmerV2) checkaxes(statespace(env), state) checkaxes(actionspace(env), action) - @uviews state begin - shapedstate = statespace(env)(state) - reward_fwd = (_torso_x(shapedstate, env) - shapedstate.last_torso_x) / timestep(env) - reward_ctrl = -1e-4 * sum(x->x^2, action) - reward_fwd + reward_ctrl - end + shapedstate = statespace(env)(state) + reward_fwd = (_torso_x(shapedstate, env) - shapedstate.last_torso_x) / timestep(env) + reward_ctrl = -1e-4 * sum(x->x^2, action) + reward_fwd + reward_ctrl end function geteval(state, action, obs, env::SwimmerV2) checkaxes(statespace(env), state) - @uviews state begin - _torso_x(statespace(env)(state), env) - end + _torso_x(statespace(env)(state), env) end @@ -122,4 +110,4 @@ function step!(env::SwimmerV2) end @inline _torso_x(shapedstate::ShapedView, ::SwimmerV2) = shapedstate.simstate.qpos[1] -@inline _torso_x(env::SwimmerV2) = env.sim.d.qpos[1] \ No newline at end of file +@inline _torso_x(env::SwimmerV2) = env.sim.d.qpos[1] diff --git a/src/gym/walker2d-v2.jl b/src/gym/walker2d-v2.jl index 3f4e350..a77435c 100644 --- a/src/gym/walker2d-v2.jl +++ b/src/gym/walker2d-v2.jl @@ -45,21 +45,17 @@ end function getstate!(state, env::Walker2DV2) checkaxes(statespace(env), state) - @uviews state begin - shaped = statespace(env)(state) - getstate!(shaped.simstate, env.sim) - shaped.last_torso_x = env.last_torso_x - end + shaped = statespace(env)(state) + getstate!(shaped.simstate, env.sim) + shaped.last_torso_x = env.last_torso_x state end function setstate!(env::Walker2DV2, state) checkaxes(statespace(env), state) - @uviews state begin - shaped = statespace(env)(state) - setstate!(env.sim, shaped.simstate) - env.last_torso_x = shaped.last_torso_x - end + shaped = statespace(env)(state) + setstate!(env.sim, shaped.simstate) + env.last_torso_x = shaped.last_torso_x env end @@ -69,7 +65,7 @@ end function getobs!(obs, env::Walker2DV2) checkaxes(obsspace(env), obs) qpos = env.sim.d.qpos - @views @uviews qpos obs begin + @views begin shaped = obsspace(env)(obs) copyto!(shaped.cropped_qpos, qpos[2:end]) copyto!(shaped.qvel, env.sim.d.qvel) @@ -82,21 +78,17 @@ end function getreward(state, action, ::Any, env::Walker2DV2) checkaxes(statespace(env), state) checkaxes(actionspace(env), action) - @uviews state begin - shapedstate = statespace(env)(state) - alive_bonus = 1.0 - reward = (_torso_x(shapedstate, env) - shapedstate.last_torso_x) / timestep(env) - reward += alive_bonus - reward -= 1e-3 * sum(x->x^2, action) - reward - end + shapedstate = statespace(env)(state) + alive_bonus = 1.0 + reward = (_torso_x(shapedstate, env) - shapedstate.last_torso_x) / timestep(env) + reward += alive_bonus + reward -= 1e-3 * sum(x->x^2, action) + reward end function geteval(state, ::Any, ::Any, env::Walker2DV2) checkaxes(statespace(env), state) - @uviews state begin - _torso_x(statespace(env)(state), env) - end + _torso_x(statespace(env)(state), env) end @@ -124,24 +116,22 @@ end function isdone(state, ::Any, ::Any, env::Walker2DV2) checkaxes(statespace(env), state) - @uviews state begin - shapedstate = statespace(env)(state) - torso_x = _torso_x(shapedstate, env) - height = _torso_height(shapedstate, env) - torso_ang = _torso_ang(shapedstate, env) - qpos = shapedstate.simstate.qpos - qvel = shapedstate.simstate.qvel - - done = !( - height > 0.8 - && height < 2.0 - && abs(torso_ang) < 1.0 - ) - done - end + shapedstate = statespace(env)(state) + torso_x = _torso_x(shapedstate, env) + height = _torso_height(shapedstate, env) + torso_ang = _torso_ang(shapedstate, env) + qpos = shapedstate.simstate.qpos + qvel = shapedstate.simstate.qvel + + done = !( + height > 0.8 + && height < 2.0 + && abs(torso_ang) < 1.0 + ) + done end @inline _torso_x(shapedstate::ShapedView, ::Walker2DV2) = shapedstate.simstate.qpos[1] @inline _torso_x(env::Walker2DV2) = env.sim.d.qpos[1] @inline _torso_height(shapedstate::ShapedView, ::Walker2DV2) = shapedstate.simstate.qpos[2] -@inline _torso_ang(shapedstate::ShapedView, ::Walker2DV2) = shapedstate.simstate.qpos[3] \ No newline at end of file +@inline _torso_ang(shapedstate::ShapedView, ::Walker2DV2) = shapedstate.simstate.qpos[3] diff --git a/src/mjsim.jl b/src/mjsim.jl index a618530..17efca6 100644 --- a/src/mjsim.jl +++ b/src/mjsim.jl @@ -146,7 +146,7 @@ Copy the following state fields from `sim.d` into `state`: @propagate_inbounds function getstate!(state::RealVec, sim::MJSim) @boundscheck checkaxes(statespace(sim), state) shaped = statespace(sim)(state) - @uviews shaped begin _copyshaped!(shaped, sim.d) end + _copyshaped!(shaped, sim.d) state end @@ -193,14 +193,14 @@ Copy the components of `state` to their respective fields in `sim.d`, namely: copystate!(sim, state) forward!(sim) shaped = statespace(sim)(state) - @uviews shaped begin copyto!(sim.d.qacc_warmstart, shaped.qacc_warmstart) end + copyto!(sim.d.qacc_warmstart, shaped.qacc_warmstart) sim end @propagate_inbounds function copystate!(sim::MJSim, state::RealVec) @boundscheck checkaxes(statespace(sim), state) shaped = @inbounds statespace(sim)(state) - @uviews shaped begin _copyshaped!(sim.d, shaped) end + _copyshaped!(sim.d, shaped) sim end @@ -350,9 +350,9 @@ function masscenter(sim::MJSim) mcntr = zeros(SVector{3, Float64}) mtotal = 0.0 body_mass, xipos = sim.m.body_mass, sim.d.xipos - @uviews xipos for i=1:sim.m.nbody + for i=1:sim.m.nbody bmass = body_mass[i] - bodycom = SVector{3, Float64}(uview(xipos, :, i)) + bodycom = SVector{3, Float64}(xipos[1,i],xipos[2,i],xipos[3,i]) mcntr = mcntr + bmass * bodycom mtotal += bmass end @@ -400,4 +400,4 @@ Return the current simulation time, in seconds, of `sim`. Equivalent to sim.d.ti Base.show(io::IO, ::MIME"text/plain", sim::Union{<:MJSim, Type{<:MJSim}}) = show(io, sim) Base.show(io::IO, sim::Union{<:MJSim, Type{<:MJSim}}) = print(io, "MJSim") -@inline check_skip(skip) = skip > 0 || throw(ArgumentError("`skip` must be > 0")) \ No newline at end of file +@inline check_skip(skip) = skip > 0 || throw(ArgumentError("`skip` must be > 0")) diff --git a/src/suite/armhand/armhandpickup.jl b/src/suite/armhand/armhandpickup.jl index e9687a7..8288dd1 100644 --- a/src/suite/armhand/armhandpickup.jl +++ b/src/suite/armhand/armhandpickup.jl @@ -100,7 +100,7 @@ end pinky = SPoint3D(sx, env.pinky) goal = ball - env.goal - @uviews obs @inbounds begin + @inbounds begin shaped = obsspace(env)(obs) shaped.ball .= ball diff --git a/src/suite/pointmass.jl b/src/suite/pointmass.jl index eaf2249..e16f4ee 100644 --- a/src/suite/pointmass.jl +++ b/src/suite/pointmass.jl @@ -31,17 +31,14 @@ function tconstruct(::Type{PointMass}, N::Integer) Tuple(PointMass(s) for s in tconstruct(MJSim, N, modelpath, skip=1)) end - @inline getsim(env::PointMass) = env.sim - - @inline obsspace(env::PointMass) = env.obsspace @propagate_inbounds function getobs!(obs, env::PointMass) @boundscheck checkaxes(obsspace(env), obs) dn = env.sim.dn shaped = obsspace(env)(obs) - @uviews shaped @inbounds begin + @inbounds begin shaped.agent_xy_pos .= dn.xpos[:x, :agent], dn.xpos[:y, :agent] shaped.agent_xy_vel .= dn.qvel[:agent_x], dn.qvel[:agent_y] shaped.target_xy_pos .= dn.xpos[:x, :target], dn.xpos[:y, :target] @@ -49,25 +46,22 @@ end obs end - @propagate_inbounds function getreward(::Any, ::Any, obs, env::PointMass) @boundscheck checkaxes(obsspace(env), obs) shaped = obsspace(env)(obs) - @uviews shaped @inbounds begin + @inbounds begin 1.0 - euclidean(shaped.agent_xy_pos, shaped.target_xy_pos) end end - @propagate_inbounds function geteval(::Any, ::Any, obs, env::PointMass) @boundscheck checkaxes(obsspace(env), obs) shaped = obsspace(env)(obs) - @uviews shaped @inbounds begin + @inbounds begin euclidean(shaped.agent_xy_pos, shaped.target_xy_pos) end end - @propagate_inbounds function randreset!(rng::Random.AbstractRNG, env::PointMass) reset_nofwd!(env.sim) @inbounds begin @@ -78,4 +72,4 @@ end end forward!(env.sim) env -end \ No newline at end of file +end