Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint_cc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ jobs:
- uses: DoozyX/[email protected]
with:
source: win/rl src/nle.c sys/unix/nledl.c include/nle.h include/nledl.h
include/nleobs.h
include/nletypes.h include/nlernd.h src/nlernd.c
clangFormatVersion: 12
4 changes: 1 addition & 3 deletions .github/workflows/test_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ jobs:
if: github.event_name != 'release'
uses: pypa/[email protected] # The main configuration is in pyproject.toml
env:
# Build of cp312-manylinux_aarch64 wheels failing, so restrict
# to only building _x86_64 wheel
CIBW_BUILD: "cp312-manylinux_*" # Build only python 3.12 wheels for testing
CIBW_BUILD: "cp312-manylinux*" # Build only python 3.12 wheels for testing
# Increase verbosity to see what's going on in the build in case of failure
CIBW_BUILD_VERBOSITY: 3
CIBW_REPAIR_WHEEL_COMMAND_LINUX: >
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ nle/nethackdir
# CMake
CMakeFiles
CMakeCache.txt
CMakePresets.json

# cibuildwheel
wheelhouse/
14 changes: 4 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,6 @@ add_compile_definitions(
NOCWD_ASSUMPTIONS
NLE_USE_TILES)

option(USE_SEEDING "Use seeding support in NLE" ON)

if(USE_SEEDING)
add_compile_definitions(NLE_ALLOW_SEEDING)
message("Seeding enabled.")
else()
message("Seeding disabled.")
endif()

set(NLE_SRC ${nle_SOURCE_DIR}/src)
set(NLE_INC ${nle_SOURCE_DIR}/include)
set(NLE_DAT ${nle_SOURCE_DIR}/dat)
Expand Down Expand Up @@ -139,7 +130,10 @@ target_link_libraries(nethack PUBLIC m fcontext bz2_static tmt)

# dlopen wrapper library
add_library(nethackdl STATIC "sys/unix/nledl.c")
target_include_directories(nethackdl PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(
nethackdl
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/third_party/deboost.context/include)
target_link_libraries(nethackdl PUBLIC dl)

# rlmain C++ (test) binary
Expand Down
4 changes: 2 additions & 2 deletions doc/nle/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ user.
- **part of `libnethack.so`**
- **key directories & files** [`src/nle.c`](https://github.com/heiner/nle/blob/main/src/nle.c),
[`include/nle.h`](https://github.com/heiner/nle/blob/main/include/nle.h),
[`include/nleobs.h`](https://github.com/heiner/nle/blob/main/include/nleobs.h)
[`include/nletypes.h`](https://github.com/heiner/nle/blob/main/include/nletypes.h)


* In our case the user “playing” the game is not a terminal, but ultimately a
Expand Down Expand Up @@ -116,7 +116,7 @@ user.:
* a struct that contains the stack type and the two contexts
(`return_context`, `generator_context`) which are the user and
nethack-stack contexts respectively.
* `nle_obs from include/nleobs.h`
* `nle_obs from include/nletypes.h`
* a struct that contains the observations transferred between the two
stacks/contexts

Expand Down
28 changes: 2 additions & 26 deletions include/nle.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,20 @@

#include <fcontext/fcontext.h>

#include "nleobs.h"
#include "nletypes.h"

/* TODO: Fix this. */
#undef SIG_RET_TYPE
#define SIG_RET_TYPE void (*)(int)

typedef struct TMT TMT;

typedef struct nle_globals {
fcontext_stack_t stack;
fcontext_t returncontext;
fcontext_t generatorcontext;

FILE *ttyrec;
TMT *vterminal;
char outbuf[BUFSIZ];
char *outbuf_write_ptr;
char *outbuf_write_end;

#ifdef NLE_BZ2_TTYRECS
void *ttyrec_bz2;
#endif

boolean done;
nle_obs *observation;
} nle_ctx_t;

/*
* Would like to annotate this with __thread, but that causes
* the MacOS dynamic linker to not unload the library on dlclose().
*/
nle_ctx_t *current_nle_ctx;

nle_ctx_t *nle_start(nle_obs *, FILE *, nle_seeds_init_t *, nle_settings *);
nle_ctx_t *nle_start(nle_obs *, FILE *, nle_settings *);
nle_ctx_t *nle_step(nle_ctx_t *, nle_obs *);
void nle_end(nle_ctx_t *);

void nle_set_seed(nle_ctx_t *, unsigned long, unsigned long, boolean);
void nle_get_seed(nle_ctx_t *, unsigned long *, unsigned long *, boolean *);

#endif /* NLE_H */
8 changes: 3 additions & 5 deletions include/nledl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <stdio.h>

#include "nleobs.h"
#include "nletypes.h"

typedef struct nledl_ctx {
char dlpath[1024];
Expand All @@ -17,12 +17,10 @@ typedef struct nledl_ctx {
FILE *ttyrec;
} nledl_ctx;

nledl_ctx *nle_start(const char *, nle_obs *, FILE *, nle_seeds_init_t *,
nle_settings *);
nledl_ctx *nle_start(const char *, nle_obs *, FILE *, nle_settings *);
nledl_ctx *nle_step(nledl_ctx *, nle_obs *);

void nle_reset(nledl_ctx *, nle_obs *, FILE *, nle_seeds_init_t *,
nle_settings *);
void nle_reset(nledl_ctx *, nle_obs *, FILE *, nle_settings *);
void nle_end(nledl_ctx *);

void nle_set_seed(nledl_ctx *, unsigned long, unsigned long, char);
Expand Down
17 changes: 17 additions & 0 deletions include/nlernd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*

Set of functions to manipulate NetHack's Random Number Generators

*/

#ifndef NLERND_H
#define NLERND_H


#include "nletypes.h"


void nle_set_seed(nle_ctx_t *, unsigned long, unsigned long, boolean);
void nle_get_seed(nle_ctx_t *, unsigned long *, unsigned long *, boolean *);

#endif
50 changes: 42 additions & 8 deletions include/nleobs.h → include/nletypes.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

#ifndef NLEOBS_H
#define NLEOBS_H
#ifndef NLETYPES_H
#define NLETYPES_H

#include <stdio.h>
#include <stdbool.h>
#include <fcontext/fcontext.h>

#define NLE_MESSAGE_SIZE 256
#define NLE_BLSTATS_SIZE 27
Expand Down Expand Up @@ -42,9 +46,19 @@
#define NLE_BL_CONDITION 25 /* condition bit mask */
#define NLE_BL_ALIGN 26

/* #define NLE_ALLOW_SEEDING 1 */ /* Set in CMakeLists.txt if not disabled. */
/* #define NLE_USE_TILES 1 */ /* Set in CMakeLists.txt. */

/* NetHack defines boolean as follows:
typedef xchar boolean; (global.h:80)
typedef schar xchar; (global.h:73)
typedef signed char schar; (config.h:420)

So we'll do the same to avoid having to include all of NetHack's types
*/
typedef signed char boolean;

typedef struct TMT TMT;

typedef struct nle_observation {
int action;
int done;
Expand Down Expand Up @@ -72,14 +86,30 @@ typedef struct nle_observation {
} nle_obs;

typedef struct {
#ifdef NLE_ALLOW_SEEDING
unsigned long seeds[2]; /* core, disp */
char reseed; /* boolean: use NetHack's anti-TAS reseed mechanism? */
#else
int _dummy; /* empty struct has size 0 in C, size 1 in C++ */
#endif
bool use_init_seeds; /* bool to tell NLE if seeds were provided */
} nle_seeds_init_t;

typedef struct nle_globals {
fcontext_stack_t stack;
fcontext_t returncontext;
fcontext_t generatorcontext;

FILE *ttyrec;
TMT *vterminal;
char outbuf[BUFSIZ];
char *outbuf_write_ptr;
char *outbuf_write_end;

#ifdef NLE_BZ2_TTYRECS
void *ttyrec_bz2;
#endif

boolean done;
nle_obs *observation;
} nle_ctx_t;

typedef struct nle_settings {
/*
* Path to NetHack's game files.
Expand All @@ -98,6 +128,10 @@ typedef struct nle_settings {
* Filename for nle's ttyrec*.bz2.
*/
char ttyrecname[4096];

/* Initial seeds for the RNGs */
nle_seeds_init_t initial_seeds;

} nle_settings;

#endif /* NLEOBS_H */
#endif /* NLETYPES_H */
11 changes: 1 addition & 10 deletions nle/tests/test_envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,6 @@ def test_rollout_no_archive(self, env_name, rollout_len):

def test_seed_interface_output(self, env_name, rollout_len):
"""Tests whether env.seed output can be reused correctly."""
if not nethack.NLE_ALLOW_SEEDING:
return # Nothing to test.
if env_name.startswith("NetHackChallenge"):
pytest.skip("Not running seed test on NetHackChallenge")

Expand All @@ -277,8 +275,6 @@ def test_seed_interface_output(self, env_name, rollout_len):

def test_seed_rollout_seeded(self, env_name, rollout_len):
"""Tests that two seeded envs return same step data."""
if not nethack.NLE_ALLOW_SEEDING:
return # Nothing to test.
if env_name.startswith("NetHackChallenge"):
pytest.skip("Not running seed test on NetHackChallenge")

Expand All @@ -302,8 +298,6 @@ def test_seed_rollout_seeded(self, env_name, rollout_len):

def test_seed_rollout_seeded_int(self, env_name, rollout_len):
"""Tests that two seeded envs return same step data."""
if not nethack.NLE_ALLOW_SEEDING:
return # Nothing to test.
if env_name.startswith("NetHackChallenge"):
pytest.skip("Not running seed test on NetHackChallenge")

Expand Down Expand Up @@ -468,9 +462,6 @@ def test_no_seed_setting(self):
RuntimeError, match="NetHackChallenge doesn't allow seed changes"
):
env.unwrapped.seed()

with pytest.raises(RuntimeError, match="Should not try changing seeds"):
env.unwrapped.nethack.set_initial_seeds(0, 0, True)

if not nethack.NLE_ALLOW_SEEDING:
with pytest.raises(RuntimeError, match="Seeding not enabled"):
env.unwrapped.nethack._pynethack.set_initial_seeds(0, 0, True)
5 changes: 0 additions & 5 deletions nle/tests/test_nethack.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ def test_several_nethacks(self, game):
game1.close()

def test_set_initial_seeds(self):
if not nethack.NLE_ALLOW_SEEDING:
return # Nothing to test.
game = nethack.Nethack(copy=True)
game.set_initial_seeds(core=42, disp=666)
obs0 = game.reset()
Expand All @@ -135,9 +133,6 @@ def test_set_initial_seeds(self):
game.close()

def test_set_seed_after_reset(self, game):
if not nethack.NLE_ALLOW_SEEDING:
return # Nothing to test.

game.reset()
# Could fail on a system without a good source of randomness:
assert game.get_current_seeds()[2] is True
Expand Down
2 changes: 0 additions & 2 deletions nle/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ def test_run_1k_steps(self, observation_keys, benchmark):
actions = np.random.choice(env.action_space.n, size=steps)

def seed():
if not nle.nethack.NLE_ALLOW_SEEDING:
return
nonlocal seeds
seeds += 1
env.unwrapped.seed(seeds, 2 * seeds)
Expand Down
Loading
Loading