{'fraction_non_alphanumeric': 0.024601712365882735, 'fraction_numerical': 0.002962320725407319, 'mean_word_length': 4.9531182795698925, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
A Discrete-event-based Simulator for Deep Learning at Edge

Novel smart environments, such as smart home, smart city, and intelligent transportation, are driving increasing interest in deploying deep neural networks (DNN) at edge devices. Unfortunately, deploying DNN on resource-constrained edge devices poses a huge challenge. If a simulator can interact with deep learning frameworks, it can facilitate researches on deep learning at edge. The existing simulation frameworks, such as Matlab, NS-3, etc., haven’t been extended to support simulations of edge learning. To support large-scale training simulations on edge nodes, we propose a discrete-event-based edge learning simulator. It includes a deep learning module and a network simulation module. Specifically, it enable simulations as an environment for deep learning. Our framework is generic and can be used in various deep learning problems before the deep learning model is deployed. In this paper, we give the design and implementation details of the discrete-event-based learning simulator and present an illustrative use case of the proposed simulator.


I. INTRODUCTION
With the breakthrough of Artificial Intelligence (AI), we are witnessing a booming increase in AI-based applications and services. The existing intelligent applications are computation intensive. To process a huge number of data in time at the edge of the network, deep learning deployment at edge has rapidly developed in recently, which takes a part of the resources and memory from the data center and puts it in edge network devices closer to end users. It can thus reduce the network transmission delay, protects user's privacy and improve the network experience of end users.
With the rapid uptake of deep learning at edge, new applications of deep learning at edge constantly come up. However, network configurations and applications are variable which have brought great difficulties to the verification of new applications. Three methods were born in response to this problem: theoretical analysis, simulation, and experimental deployment. As an intermediate stage between the theoretical analysis and experimental deployment of edge learning methods, simulation is more flexible and low-cost. It can be used for preliminary verification and implementation of the new applications of edge learning [1] [2]. At present, there are some simulation frameworks at edge, such as MatLab, NS-3, etc. However, these frameworks have not meet current needs of learning at edge. Among them, MatLab does not support systematic simulation methods considering discrete events [3] [4], so that the effectiveness of edge learning simulation on MatLab cannot be guaranteed. As a network simulator, NS- 3 has not yet been involved in the simulations for edge learning. The combination of network simulation and deep learning algorithms faces challenges in the actual training and testing process.
To solve the existing problems and facilitate large-scale training on the edge simulation platform, we incorporate the deep learning module into NS-3 to build an discrete-eventbased edge learning simulator (sim4edgeDL 1 ). The proposed simulator, sim4edgeDL, supports the simulation of discrete events. Users can choose C++ or Python to write scripts to configure the simulation environment according to their own needs and implement different network functions. sim4edgeDL has high performance, since it can be continuously optimized in a parall way to enhance its efficiency. This framework has good design and versatility, including various algorithms and utilities for classification, regression, prediction and other artificial intelligence solutions. It can create the models required for many problems. This paper is organized as follows. Section 2 introduces related work, Section 3 introduces system architecture, Section 4 introduces implementation details, Section 5 introduces an example and specific implementation details, and Section 6 concludes this article.

II. RELATED WORK
As a brand-new technology under development, the simulator OPNET, QualNet, and REAL cannot be widely used due to restrictions in terms of use or high R&D costs.
Previously, the performance verification of edge learning schemes is still based on Matlab simulation. Initially, Yuqing Du et al. [5] conducted research and verification on the new hierarchical gradient quantization framework proposed on the MatLab simulation platform, which solved the communication overhead problem in edge learning. Later, Wen Sun et al. [6] integrated intelligence into edge computing and verified the proposed IIoT intelligent computing architecture with collaborative edge and cloud computing on the MatLab platform. Ali Hassan Sodhro et al. [7] also used MatLab to emulate deep learning at the edge. However, MatLab lacks systematic simulation methods, such as discrete event simulation, cannot accurately characterize the impact of network transmission performance on the learning process in a distributed environment, and cannot guarantee the validity of edge learning performance verification.
NS-2 is essentially a discrete event simulator, which solves the influence of network transmission performance on learning. Rubayyi Alghamdi et al. [8] presented an edge-cloud deep IDS model in Lambda architecture for IoT security to address these challenges. And they verified the solution using NS-2. Rongxia Wang et al. [9] also verified the proposed Intrusion Detection Technology of the Internet of Vehicles Based on Deep Learning on the NS-2 simulation platform. The number of functional modules in the NS-2 package is large and easy to expand. Still, the interoperability and coupling between modules are poor, and module incompatibility problems are likely to occur when the version is updated. At the same time, the simulation speed is slower than other general-purpose simulation software.
NS-3 combines the advantages of NS-2 and other simulators to develop a new open-source, cross-platform discreteevent network simulator for Internet systems [10]. It supports distributed simulation and parallel simulation and runs faster. Ns-3 is designed as a set of libraries that can be combined with other external software libraries [11]. It became a standard in networking research as the results obtained were accepted by the community. Piotr Gawłowicz et.al [12] have developed a module, NS3-gym, to provide an interface in Reinforced Learning between ns and OpenAI Gym. It uses the API provided by the OpenAI gym. They use ZMQ socket to implement the communication between NS-3 and OpenAI gym. However, facing the next generation of networks, the network's density is rapidly increasing. A large amount of data may require to transmit from ns to AI modules for training or testing. It is crucial to reduce the time-consuming and support colossal data transmission, which sockets can do. Hao Yin et al. [13]proposed NS3-AI to connect AI algorithms and ns simulators with data interaction. They use shared memory to achieve an artificial combination of intelligent algorithms and network simulation. The use of shared memory dramatically reduces the time consumption in large-scale data transmission. Still, there are many pitfalls in using shared memory, which can easily lead to program crashes. To solve the above problems, we construct a discrete-event-based edge learning simulator (sim4DEL).

III. SYSTEM ARCHITECTURE
The architecture of sim4DEL, as depicted in Fig. 1, consists of the following major components: network simulation module and deep learning module. The former simulates the caching and transmission of data in the edge network, while the latter implements deep learning model training based on cached data. The main contribution of this work is the design and implementation of the sim4DEL that allows for seamless integration of those two modules. Among them, the sequence diagram of running deep learning algorithms in network simulator is shown in Fig. 2. In the following subsections, we describe sim4DEL components in detail.

A. sim4DEL
The simulation core of sim4DEL is implemented in the core module. We will introduce from the bottom up; in general, modules only have dependencies on modules beneath them in Fig. 1.
The core module includes a smart pointer, dynamic type system, callback, tracking, logging, random variables events, scheduler, time calculation, and so on. In addition, the primary object in the network simulator -data packet is implemented in the network module. It includes the methods implemented by the following functions: node class, network device, MAC address type (IPv4), queue, socket, IPv4 / IPv6 address, packet, etc. The upper layer of the network module includes the internet module and the mobility module. The internet module consists of the implementation method of network protocols, topology, and devices. The mobility module consists of the implementation method of equipment and data transmission in the network. The upper layer of the internet module implements the protocol module and topology module, in which TCP / IP network protocol is implemented in the protocol module. The topology module sets the specific network topology according to the specific edge network. The device module and the propagation module are implemented module on the Mobility module. The critical function of the device module is to design the method of LRU-based Caching on the device module. The function of the application is implemented in the edge network. The learning process of the model is supported by customizing the learning application. Learning Application as a middle module interconnects network simulator and deeplearning framework.
The deep learning framework specifically includes six core modules: Data Set module, Neural Network module, Training Strategy module, Training module, Testing module, and Learning models module. The details of these modules are described below.
The Data Set module contains application modules for data processing. The Neural Network module is a module that defines the depth structure, which brings together all the different depth structures. The Training Strategy module is an in-depth training strategy module. The Training module is a module of the in-depth training process. It is used to train tools in different learning tasks. We deploy the Training module in the form of a custom Application on the network mentioned above discrete event generator, supporting deep learning and other learning processes on the TCP/IP edge network. The Testing module is a module of the testing process, which contains tools for testing in other learning tasks. The learning module is used for deep learning on the edge network.
There is a dependency relationship between modules, and the relationship between modules and modules constitutes the core module of deep learning. Among them, TrainingStrategy handles Dataset and NeuralNetwork to perform neural network training; ModelSelection uses TrainingStrategy to check which model gets the best results; TestingAnalisys evaluate NeuralNetwork over a DataSet.
DataSet is not composed of classes. It is a highlevel class where all methods are controlled by it. The classes in NeuralNetwork include PerceptronLayer, Scal-ingLayer, UnscalingLayer, Boundinglayer, ProbabilisticLayer, LongShortMemoryLayer, RecurrentLayer, ConvolutionaLayer, PoolingLayer, PrincipalComponentsLayer. TrainingStrategy includes the LossIndex class and the OptimizationAlgorithm class. LossIndex class represents the concept of the error term. The OptimizationAlgorithm class represents the neural network training algorithms. The LossIndex class derives some classes. These are methods for calculating errors, such as mean square error, that need to be set during neural network training. It is derived from the OptimizationAlgorithm class to select the neural network training method, e.g., stochastic gradient descent. The basic classes of ModelSelection include InputSelection and NeurosSelection. The abstract class InputSelection represents the input selection algorithms of the neural network. The abstract class NeuronsSelection represents the neuron selection algorithms of neural networks.

B. Network Simulation Module
The network simulation module is a core part of our framework since it is used to implement a simulation scenario serving as an environment for distributed learning by setting up the network and topology. A simulation scenario contains data transmission and data caching in simulation conditions. One can create even very complex network topology and study them.
1) Core Functions of Network Simulation Module: The network simulation module is based on discrete event scheduling. It fulfills the needs of modern networking research. The network simulation module uses several core concepts and abstractions that can be well mapped to the natural network environment. Nodes are the most basic connected entities. Various functions can be added to the node, e.g, applications,LRUbased caching. It's a container class for applications, channels, and devices. The application program in the simulation environment runs on the node to drive the simulation process. It is necessary to abstract the simulated user program as an application described by the Application class. The Channel class provides methods for managing communication sub-net objects and connecting various nodes. The Device class offers multiple strategies for managing connections between other nodes and channel objects. Meanwhile, it allows developers to define them in an object-oriented way. With network simulator, users can create their simulation typologies with custom node and link parameters, simulate the whole TCP/IP protocol stack (physical, link, network, transport, application layer protocols), trace and collect simulation data and visualize the simulation execution. The network simulation module can also interact with applications (such as using direct code execution technology) and external libraries. All these features are essential for the research of the network and the application of deep learning algorithms.
It is up to the designer to decide which part of the simulation state is going to be exposed to the deep learning model for the learning purpose. Generally, it will be a sub-set of the caching data and some parameters during the train step execution (e.g., parameters after aggregating neighbor nodes). To build and support the deep learning module on the simulation framework, we designed and implemented the cache module based on the least recently used (LRU) in the Network Simulation Module and provided an interface for the deep learning module.
2) Design of LRU-based Caching: The data transmission in the simulation module mainly sends data for training and testing of the deep learning model. Data is the input of deep learning, and the cache is the carrier of data. It is essential to design and implement a excellent caching. LRU (Least Recently Used) is currently the most commonly used cache replacement algorithm. It's core idea is "If the data has been accessed recently, the probability of being accessed in the future will also be higher". We design a LRU-based Caching module and include it in the Network module.
The construction of the LRU-based Caching module requires a parameter configuration method such as the cache size. LRU-based Caching is based on the data packet in the data transmission process as a unit. In addition, the unit of the LRU cache module designed in this article can be changed through overloaded functions according to requirements. The LRU-based Caching module uses a linked list form. The least recently used data is placed at the head of the linked list. The addition, deletion and search functions of cached data maintain this design. The specific operations are described as follows: 1) To cache data, we first judge whether the currently stored data has reached the cache capacity. If it has reached the cache capacity, delete the last data in the linked list and store it according to the algorithm principle; If the cache capacity is not reached, it is directly stored in the head of the linked list. 2) In the process of removing cached data, when the cache capacity is insufficient, the last data in the linked list represents the most recently unused data, and it is deleted. 3) In the process of looking up the cached data, if the data is not found, -1 is returned; otherwise, the value of the data is returned. Then the data is placed at the head of the linked list.

C. Deep Learning Module
The main purpose of the deep learning module is to provide a standardized interface allowing to execution of deep learning in an environment. Note that the environment is defined entirely inside the simulation scenario.
The main purpose of the deep learning module is to provide standardized deep learning algorithms for simulation. It is a deep library for machine learning. It is mainly oriented to the field of deep learning. It helps users build various deep models that can be used to implement non-linear models at any level in supervised learning scenarios while also supporting various in-depth designs with general approximate properties.

IV. IMPLEMENTATION
The sim4DEL is a framework that consists of two components (network simulation module and the deep learning module). We have released the source code of sim4edgeDL in an online repository 2 . The sim4DEL simplifies the tasks of the development of deep learning in networking environments. Specifically, sim4DEL provides a way to collect and exchange information between network simulation module and deep learning module, and simulate the deep learning in the network simulation.
In the following subsections, we describe the sim4DEL components in detail, introduce the implementation process of the module in the simulation, and introduce the deployment of sim4DEL.

A. Implementation of the LRU-based Caching
We built a new LRU-based Caching module based on the existing core module. Use the file waf to automatically compile this new module to achieve the purpose of correctly finding the header file. The creation of the LRU-based Caching module in the simulation environment is divided into the following four sub-steps: 1) LRU-based Caching module construction First, we need to create a new module to provide conditions for the placement of the cache code. In the directory of the Core module, use terminal commands to customize the new module. The terminal commands are as follows: ./utils/create-module.py src/Lru Among them, Lru is the name of a custom cache module. The create-module.py command helps to create a directory structure. The system will automatically create a subdirectory named after Lru. Its internal structure includes several modules such as module and wscript.

2) Configuration
After the new module is created, the wscript configuration file needs to be modified. The wscript file structure is fixed; it is used to register the source code contained in the project and reference other modules.
In the wscript file, we need to set the modules in the Core that the cache module needs to rely on. The specific dependent modules are used to help judge the header files quoted in the code files. In addition, we need to use module.source and headers.source to declare the included cc files and h files, respectively.

3) Add a custom LRU-based Caching module to the Core module
After the cache module is established, it needs to be added to the core module of the simulation to facilitate the use of cached data in the simulation scenario. To add the LRU-based Caching module to the simulation environment, we need to use waf and configure to recompile and configure the Core module. ./waf configure ./waf build Among them, waf is a compilation tool developed based on python, and the code in the simulation environment is compiled by waf. The command build is the compilation directory, which contains the shared libraries and header files used when compiling files, and the path that must be entered when compiling scripts with the waf tool. 4) Deploy the LRU-based Caching module on the edge node After the cache module is successfully implemented, it needs to be deployed in the edge simulation node. The attributes of the edge node are defined in the node class, and the LRU-based Caching needs to be added as a member variable of the Node class so that the cache is deployed in the edge node. A custom cache module can use the namespace of the Core module or a custom namespace. Writing code involves caching, and we only need to quote its header files.
Through the above operations, the construction of the LRUbased Caching module is implemented in the form of a custom module, and it is added to the core module. At the same time, the cache data function of edge network node is implemented.

B. Implement of the deep learning module
At present, most deep learning models rely on open-source frameworks, such as TensorFlow and PyTorch, which creates specific difficulties for building deep learning models and verifying algorithms. The current network simulators and deep learning frameworks are developed independently. In detail, we have built a deep learning library that does not rely on third-party frameworks. Take LeNet as an example here and import it into the deep learning module. The following is the specific method of adding the LeNet model to the deep learning library.
First, we need to design and implement the code of LeNet. Then we need the following steps to import LeNet's source code into the deep learning module.

1) Use a unified coding format, use the cc and h formats
to implement the LeNet model. 2) Include all the header files that implement the LeNet model into the total file of the deep learning library so that a new neural network model is imported into the deep learning library. 3) When using LeNet, we only need to import the header file in the program.

C. Deployment of sim4DEL
To turn a simulation scenario into a deep learning environment, one needs to i) integrate and deploy the designed deep learning module with discrete event simulation ii) the model training method is used as a new Application. The specific deployment process is as follows: 1) The Eigen library is a C++ template library for linear operations, which can support matrix and vector operations, numerical analysis, and related algorithms, and the deep learning module contains a large number of matrix operations, so we need to use Eigen's template library. Import the Eigen in the deep learning module into the network simulation module, the preliminary step of the joint compilation of the deep learning module and the network simulation module. 2) After creating the conditions for joint compilation of the deep learning and simulation, it is necessary to integrate the deep learning module and the network simulation module. First, the code of the deep learning module needs to be transplanted to the network simulation module. Secondly, we need to configure the wscript file in the Core module of the network simulation module. The main work is to add the deep learning library to the network simulation module. The configuration code is as follows: • def build(bld):

A. Topological structure construction
In this section, we present an ensemble learning problem as an use case of the proposed simulator. We first introduce the edge network topology required for the experiment, the construction of the neural network, the steps of the simulation in the simulation environment, and the specific implementation details.
This section mainly introduces the construction of the topology structure in the simulation process. It includes a remote data center, a gateway node, two edge computing nodes, and four-terminal devices connected through a gigabit link.
The data transmission method between them is Point-to-point transmission. Peer-to-Peer (P2P), also known as peer-to-peer network technology, is a new network technology that relies on the computing power and bandwidth of the participants in the network instead of concentrating all the dependencies on fewer servers. The topology is shown in the figure below: The data required for model training are all released by the terminal equipment node. The terminal device generates the learning data of the model and sends the data to the edge computing node. After the edge computing node receives the data, it first performs data caching, then uses the different data in cache to train the sub-model. Finally, it sends the training results of the sub-model to the data center for deep learning.

B. Implementation Details
The realization of edge ensemble learning is mainly divided into two parts: one is to build a neural network model using deep learning module, the other one is to implement simulation in the simulation environment.
According to the previously designed discrete-event-based edge learning simulator, we can train the deep learning model in the simulation environment. Here we adopt the LeNet model to design and implement above.
To implement the simulation in the network simulation module, it is necessary to introduce the header file of the deep learning module to use the training model. To facilitate the completion of the neural network training on the simulation node, we first encapsulate the LeNet into an application, which contains the configuration of the neural network and training parameters, such as the maximum training round, loss target, etc. To use the simulation module, its core header file and custom cache module need to be included. The specific implementation steps are as follows: 1) Generate a node, use the Create() function in the Node-Container class to create the required node. 2) Install network devices, implement the physical connection of nodes through point-to-point channels, and set data transmission rate. 3) Install the protocol stacks for the nodes, mainly configure the IP address for each node. 4) Set the sender and receiver, each sender needs to set the receiver. According to the created topology, the receiver is the local node, and the sender is the neighbor node. 5) Install Application on the nodes participating in the training, and at the same time initialize the cache size for each node and store the data in the cache one by one.
After starting the simulation, each node needs to judge whether the locally cached data is enough before training. If it is not enough, it needs to request the cached data of the neighbor nodes. The neighbor nodes send the data through the socket, and the local node uses the callback function to receive the data, and then the node call Application for neural network training. Each node sends training results to the data center for deep learning.

VI. CONCLUSIONS
In this paper, we presented the sim4DEL framework that simplifies the usage of deep learning to solve simulation problems. This is achieved by interconnecting the deep learning module with the network simulation module. As the framework is generic, the community can use it in a variety of distributed deep learning on edge network nodes. This paper also shows the specific details of building a simulation of an edge learning on the simulation framework. The edge deep learning simulation framework can build a deep learning environment similar to the real TCP/IP edge network and simulate the data transmission, caching, and model training, providing a prerequisite for edge intelligent simulation.
For the future, we plan to include more types of neural network models, which can be used to benchmark different AI techniques.